当我用它运行Valgrind并同时check50向我显示内存泄漏时,我在CS50x pset 5拼写器中收到内存泄漏错误

时间:2020-05-21 08:00:17

标签: c memory-leaks cs50

对于cs50以外的人,这是C语言中的程序,用于检查字典中每个单词的拼写。

首先,我将发布我的dictionary.c代码:

<MainWindow>:
name: "main"

FloatLayout:
    Label:
    font_name: 'Arial'  
        text:"Asad"
        font_size: (root.width**2 + root.height**2) / 13**4
        pos_hint: {"x":0.1, "top":0.9}
        size_hint: 0.35, 0.15
    Button:
        pos_hint:{"x":0.2, "y": 0.3}
        size_hint:0.6,0.2
        text: "aaa"
        on_release:
            root.new_form()
            root.manager.transition.direction = "down"

    Button:
        pos_hint:{"x":0.2, "y": 0.1}
        size_hint:0.6,0.2
        text: "aaa"
        on_release:
            root.exist_form()
            root.manager.transition.direction = "left"



<new_form_window>:
name: "new_form"

project_name: project_name
manage_name: manage_name

FloatLayout:

    Label:
        text:"aaa"
        font_size: (root.width**2 + root.height**2) / 13**4
        pos_hint: {"x":0.1, "top":0.9}
        size_hint: 0.35, 0.15

    TextInput:
        id: project_name
        font_size: (root.width**2 + root.height**2) / 13**4
        multiline: False
        pos_hint: {"x": 0.45 , "top":0.9}
        size_hint: 0.4, 0.15

    Label:
        text:"aaa"
        font_size: (root.width**2 + root.height**2) / 13**4
        pos_hint: {"x":0.1, "top":0.7}
        size_hint: 0.35, 0.15

    TextInput:
        id: manage_name
        font_size: (root.width**2 + root.height**2) / 13**4
        multiline: False
        pos_hint: {"x": 0.45, "top":0.7}
        size_hint: 0.4, 0.15

    Button:
        pos_hint:{"x":0.2,"y":0.05}
        size_hint: 0.6, 0.2
        font_size: (root.width**2 + root.height**2) / 13**4
        text: "aaa"
        on_release:
            root.manager.transition.direction = "left"
            root.create()

<exist_form_window>:
name: "exist_form"

project_name: project_name
manage_name: manage_name

FloatLayout:

    Label:
        text:"aaa"
        font_size: (root.width**2 + root.height**2) / 13**4
        pos_hint: {"x":0.1, "top":0.9}
        size_hint: 0.35, 0.15

    TextInput:
        id: project_name
        font_size: (root.width**2 + root.height**2) / 13**4
        multiline: False
        pos_hint: {"x": 0.45 , "top":0.9}
        size_hint: 0.4, 0.15

    Label:
        text:"aaa"
        font_size: (root.width**2 + root.height**2) / 13**4
        pos_hint: {"x":0.1, "top":0.7}
        size_hint: 0.35, 0.15

    TextInput:
        id: manage_name
        font_size: (root.width**2 + root.height**2) / 13**4
        multiline: False
        pos_hint: {"x": 0.45, "top":0.7}
        size_hint: 0.4, 0.15

    Button:
        pos_hint:{"x":0.2,"y":0.05}
        size_hint: 0.6, 0.2
        font_size: (root.width**2 + root.height**2) / 13**4
        text: "aaa"
        on_release:
            root.manager.transition.direction = "left"
            root.create()

当我为new_node和new_node分配空间时,问题出在加载函数中 new_node-> word。现在,我将发布错误消息:

check50错误:

File "C:\Users\adi\Anaconda3\envs\project_2\lib\site-packages\kivy\lang\parser.py", line 584, in 
parse_level
 'Invalid data after declaration')

ParserException: Parser: File "<inline>", line 6:
...
      4:    FloatLayout:
      5:        Label:
>>    6:        font_name: 'Arial'  
      7:            text:"Asad"
      8:            font_size: (root.width**2 + root.height**2) / 13**4
...
Invalid data after declaration

valgrind错误:

#include <stdbool.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <cs50.h>

#include "dictionary.h"

#define HASHTABLE_SIZE 65536

int strcasecmp(const char *s1, const char *s2);

// Represents a node in a hash table
typedef struct node
{
    char *word;
    struct node *next;
}
node;

node *new_node;
int dic_size = 0;

// Hash table
node *hashtable[HASHTABLE_SIZE];

// credit for hash function: https://gist.github.com/yangrq1018/e769abd7489ea8f52351b30829796a15
// Hashes word to a number
int hash(const char *word)
{
    unsigned int hash = 0;
    for (int i = 0, n = strlen(word); i < n; i++)
        hash = (hash << 2) ^ word[i];
    return hash % HASHTABLE_SIZE;
}

// Returns true if word is in dictionary else false
bool check(const char* word)
{
    // allocate memory to our checker
    char lword[strlen(word) + 1];
    strcpy(lword, word);
    for (int i = 0; word[i] != '\0'; i++)
    {
        lword[i] = tolower(lword[i]);
    }

    // determine in which bucket is the word
    int bucket = hash(lword);

    // situation of the word in our checker
    node *checker = hashtable[bucket];
    if (checker != NULL)
    {
        while (checker != NULL)
        {
            // use strcasecmp to be case insensitive
            if (strcasecmp(checker->word, lword) == 0)
                return true;
            checker = checker->next;
        }
    }
    return false;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // TODO

    FILE *dict = fopen(dictionary, "r");
    if (dict == NULL)
        return false;

    //make an array of chars to store the lower-case words
    char word[LENGTH + 1];
    while (fscanf(dict, "%s", word) != EOF)
    {
        new_node = malloc(sizeof(node));
        if (new_node == NULL)
        {
            fclose(dict);
            return false;
        }

        new_node->word = malloc(strlen(word) + 1);
        strcpy(new_node->word, word);
        int index = hash(tolower(word));

        new_node->next = hashtable[index];
        hashtable[index] = new_node;

        dic_size++;
    }
    fclose(dict);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    return dic_size;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // TODO
    for (int i = 0; i < HASHTABLE_SIZE; i++)
    {
        if (hashtable[i] != NULL)
        {
            node *cursor;

            cursor = hashtable[i];
            while (cursor != NULL)
            {
                node *tmp = cursor;
                cursor = cursor->next;
                free(tmp);
                return true;
            }
            free(hashtable[i]);
        }
        else
        {
            continue;
        }
    }
    return false;
}

我没有发布拼写错误的单词,因为有1000多个单词。问题发生的地方是第87行(:( program is free of memory errors valgrind tests failed; rerun with --log for more information. running valgrind --show-leak-kinds=all --xml=yes --xml-file=/tmp/tmp7cpretwh -- ./speller substring/dict substring/text... checking for output "MISSPELLED WORDS\n\nca\ncats\ncaterpill\ncaterpillars\n\nWORDS MISSPELLED: 4\nWORDS IN DICTIONARY: 2\nWORDS IN TEXT: 6\n"... checking that program exited with status 0... checking for valgrind errors... 4 bytes in 1 blocks are definitely lost in loss record 1 of 3: (file: dictionary.c, line: 87) 12 bytes in 1 blocks are still reachable in loss record 2 of 3: (file: dictionary.c, line: 87) 16 bytes in 1 blocks are still reachable in loss record 3 of 3: (file: dictionary.c, line: 80) To see the results in your browser go to https://submit.cs50.io/check50/14a6395544b18506952fa12c9d56a70e89a728d2 )和第80行(WORDS MISSPELLED: 17062 WORDS IN DICTIONARY: 143091 WORDS IN TEXT: 376904 TIME IN load: 1.77 TIME IN check: 4.26 TIME IN size: 0.00 TIME IN unload: 0.00 TIME IN TOTAL: 6.04 ==2489== ==2489== HEAP SUMMARY: ==2489== in use at exit: 3,728,668 bytes in 286,181 blocks ==2489== total heap usage: 286,187 allocs, 6 frees, 3,739,004 bytes allocated ==2489== ==2489== 10 bytes in 1 blocks are definitely lost in loss record 1 of 3 ==2489== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==2489== by 0x4012E3: load (dictionary.c:87) ==2489== by 0x4009B4: main (speller.c:40) ==2489== ==2489== 1,439,218 bytes in 143,090 blocks are still reachable in loss record 2 of 3 ==2489== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==2489== by 0x4012E3: load (dictionary.c:87) ==2489== by 0x4009B4: main (speller.c:40) ==2489== ==2489== 2,289,440 bytes in 143,090 blocks are still reachable in loss record 3 of 3 ==2489== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==2489== by 0x4012A2: load (dictionary.c:80) ==2489== by 0x4009B4: main (speller.c:40) ==2489== ==2489== LEAK SUMMARY: ==2489== definitely lost: 10 bytes in 1 blocks ==2489== indirectly lost: 0 bytes in 0 blocks ==2489== possibly lost: 0 bytes in 0 blocks ==2489== still reachable: 3,728,658 bytes in 286,180 blocks ==2489== suppressed: 0 bytes in 0 blocks ==2489== ==2489== For counts of detected and suppressed errors, rerun with: -v ==2489== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) )。我可能在哪里出错了?

0 个答案:

没有答案