Попытка выяснить, что делать, чтобы исправить утечки памяти. он говорит, что у меня есть 726160 байт в 382 блоках с определенными потерями. Я попытался пройти свою программу и нашел, что она находится на линии, где находится моя память malloc, но я не могу понять, почему. Линия:Valgrind Memory Leak Fix
int ** pixels = (int **) malloc(*numCols * sizeof(int));
Вот мой отчет Valgrind:
doe-MacBook:hw34 doe$ valgrind ./a.out -c 450 228 40 ./balloons.ascii.pgm balloon.pgm
==601== Memcheck, a memory error detector
==601== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==601== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==601== Command: ./a.out -c 450 228 40 ./balloons.ascii.pgm balloon.pgm
==601==
==601== Invalid write of size 8
==601== at 0x100000989: pgmRead (pgmUtility.c:28)
==601== by 0x100001A79: main (main.c:112)
==601== Address 0x100820e30 is 0 bytes after a block of size 2,560 alloc'd
==601== at 0x1000076C1: malloc (vg_replace_malloc.c:303)
==601== by 0x100000941: pgmRead (pgmUtility.c:26)
==601== by 0x100001A79: main (main.c:112)
==601==
==601== Invalid read of size 8
==601== at 0x100000A01: pgmRead (pgmUtility.c:32)
==601== by 0x100001A79: main (main.c:112)
==601== Address 0x100820e30 is 0 bytes after a block of size 2,560 alloc'd
==601== at 0x1000076C1: malloc (vg_replace_malloc.c:303)
==601== by 0x100000941: pgmRead (pgmUtility.c:26)
==601== by 0x100001A79: main (main.c:112)
==601==
==601== Invalid read of size 8
==601== at 0x100000B03: pgmDrawCircle (pgmUtility.c:43)
==601== by 0x100001AB7: main (main.c:114)
==601== Address 0x100820e30 is 0 bytes after a block of size 2,560 alloc'd
==601== at 0x1000076C1: malloc (vg_replace_malloc.c:303)
==601== by 0x100000941: pgmRead (pgmUtility.c:26)
==601== by 0x100001A79: main (main.c:112)
==601==
==601== Invalid read of size 8
==601== at 0x100000C20: pgmDrawCircle (pgmUtility.c:57)
==601== by 0x100001AB7: main (main.c:114)
==601== Address 0x100820e30 is 0 bytes after a block of size 2,560 alloc'd
==601== at 0x1000076C1: malloc (vg_replace_malloc.c:303)
==601== by 0x100000941: pgmRead (pgmUtility.c:26)
==601== by 0x100001A79: main (main.c:112)
==601==
Successfully wrote image to new file
==601== Invalid read of size 8
==601== at 0x1000012BE: pgmWrite (pgmUtility.c:123)
==601== by 0x100001AE3: main (main.c:115)
==601== Address 0x100820e30 is 0 bytes after a block of size 2,560 alloc'd
==601== at 0x1000076C1: malloc (vg_replace_malloc.c:303)
==601== by 0x100000941: pgmRead (pgmUtility.c:26)
==601== by 0x100001A79: main (main.c:112)
==601==
==601==
==601== HEAP SUMMARY:
==601== in use at exit: 1,267,658 bytes in 1,065 blocks
==601== total heap usage: 1,149 allocs, 84 frees, 1,284,570 bytes allocated
==601==
==601== LEAK SUMMARY:
==601== definitely lost: 726,160 bytes in 382 blocks
==601== indirectly lost: 0 bytes in 0 blocks
==601== possibly lost: 0 bytes in 0 blocks
==601== still reachable: 507,136 bytes in 263 blocks
==601== suppressed: 34,362 bytes in 420 blocks
==601== Rerun with --leak-check=full to see details of leaked memory
==601==
==601== For counts of detected and suppressed errors, rerun with: -v
==601== ERROR SUMMARY: 111418 errors from 5 contexts (suppressed: 0 from 0)
Если необходима дополнительная информация, дайте мне знать.
Вот метод, который получает сообщение об ошибке:
int ** pgmRead(char **header, int *numRows, int *numCols, FILE *in ){
int i, j;
for(i = 0; i < 4; i++)
fgets(header[i], 100, in);
rewind(in);
char x[100];
fgets(x,100, in);
fgets(x, 100, in);
int A=0;
fscanf(in, "%d %d", numCols, numRows);
fscanf(in, "%d",&A);
int ** pixels = malloc(*numCols * sizeof(int*));
for(i = 0; i < *numCols; i++){
pixels[i] = malloc(sizeof(int) * (*numRows));
}
for(j = 0; j < *numRows; j++){
for(i = 0; i < *numCols; i++){
fscanf(in, "%d", &pixels[i][j]);
}
}
return pixels;
}
я свободен, в основном, потому что, как я называю это по
pixels = pgmRead(header, &numRows, &numCols, fp);
затем I свободных (пикселей) в главном
У вас есть соответствующий вызов 'free' в вашем коде? –