Утечка памяти внутри вложенного пока l oop, хотя я освобождаю после обоих циклов - PullRequest
0 голосов
/ 17 февраля 2020

Вальгринд говорит мне, что у меня просачивается память, где «результат» расположен в двух разных местах. Я бесплатно результат в моем коде. Я действительно застрял. Интересно, что комментирование второй бесплатной версии не усугубляет утечку памяти, то есть фактически ничего не делает. Мысли?

void index_build(const char *pathname, index_t *index) 
{
    char *filename = count_malloc(strlen(pathname) + 5);
    int id = 1;
    sprintf(filename, "%s/%d", pathname, id);
    FILE *fp = fopen(filename, "r");
    while (fp != NULL) {
        char *url = count_malloc(200);
        fgets(url, 50000, fp);
        char *depth = count_malloc(sizeof(fgets(depth, 5000000, fp)) + 1);
        fgets(depth, 500000, fp);
        int d = atoi(depth);
        webpage_t *page = pageDir_load(url, d);
        int pos = 0;
        char *result = count_malloc(sizeof(webpage_getNextWord(page, &pos)) + 1);
        result = webpage_getNextWord(page, &pos);
        while (result != NULL) {
            if (index_find(index, result) == NULL) {
                counters_t *new = counters_new();
                counters_add(new, id);
                index_insert(index, result, new);
            }
            else {
                counters_t *current = index_find(index, result);
                counters_add(current, id);
            }
            free(result);
            result = count_malloc(sizeof(webpage_getNextWord(page, &pos)) + 1);
            result = webpage_getNextWord(page, &pos);
        }
        free(result);
        id++;
        fclose(fp);
        sprintf(filename, "%s/%d", pathname, id);
        fp = fopen(filename, "r");
        free(depth);
        webpage_delete(page);
    }
    free(filename);
}

Ошибка Valgrind:

54 bytes in 6 blocks are definitely lost in loss record 1 of 2
==3516925==    at 0x483980B: malloc (vg_replace_malloc.c:309)
==3516925==    by 0x40227D: count_malloc (memory.c:54)
==3516925==    by 0x401572: index_build (indexer.c:63)
==3516925==    by 0x40146C: main (indexer.c:46)
==3516925== 
==3516925== 495 bytes in 55 blocks are definitely lost in loss record 2 of 2
==3516925==    at 0x483980B: malloc (vg_replace_malloc.c:309)
==3516925==    by 0x40227D: count_malloc (memory.c:54)
==3516925==    by 0x40161B: index_build (indexer.c:76)
==3516925==    by 0x40146C: main (indexer.c:46)

1 Ответ

1 голос
/ 17 февраля 2020

Я grep'd ваш код для всех mallo c и освобождается следующим образом:

egrep "malloc|free"

Я получил эти результаты:

char *filename = count_malloc(strlen(pathname) + 5);
    char *url = count_malloc(200);
    char *depth = count_malloc(sizeof(fgets(depth, 5000000, fp)) + 1);
    char *result = count_malloc(sizeof(webpage_getNextWord(page, &pos)) + 1);
        free(result);
        result = count_malloc(sizeof(webpage_getNextWord(page, &pos)) + 1);
    free(result);
    free(depth);
free(filename);

Выше я вижу, что url никогда не бывает бесплатным.

...