簡體   English   中英

Valgrind內存泄漏修復

[英]Valgrind Memory Leak Fix

試圖弄清楚該如何解決我的內存泄漏。 它說我在382個絕對丟失的塊中有726160字節。 我試圖遍歷我的程序,發現它位於我的malloc內存所在的行,但是我不知道為什么。 該行是:

   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);

然后我主要釋放(像素)

順便說一句,在行

int ** pixels = (int **) malloc( *numCols * sizeof(int));

我認為malloc的說法不正確。

int ** pixels = malloc( numCols * sizeof(int*));

似乎應該使用什么。

更新

要解除分配數據,您必須像對malloc一樣精確地調用free

for(i = 0; i < numCols; i++){
   free(pixels[i];
}
free(pixels);

代碼不能“僅僅”釋放(像素);

這是您的問題所陳述的。

而是必須將已分配給pixel [i]的每個指針傳遞給free。 IE

for( i = 0; i < numcols; i++ )  free( pixels[i];

然后最后:

free( pixels );

請注意,.pgm圖像文件的格式與所發布的代碼不符。

建議閱讀: http : //netpbm.sourceforge.net/doc/pgm.html

http://en.wikipedia.org/wiki/Netpbm_format

還要注意,圖像以像素行(列)布置,行彼此相鄰。

所發布的代碼將圖像視為已列出像素的第一列,然后列出了第二列,依此類推。這是不正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM