我正在運行valgrind查找內存泄漏。我在主函數中分配了兩個全局變量;然後,在main
我解放了,但Valgrind的不斷書寫結束:Valgrind和全局變量
==18311== 16 bytes in 1 blocks are definitely lost in loss record 1 of 2
==18311== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==18311== by 0x804A30C: main (application.c:730)
==18311==
==18311== 16 bytes in 1 blocks are definitely lost in loss record 2 of 2
==18311== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==18311== by 0x804A31D: main (application.c:731)
泄漏摘要:
==18311== LEAK SUMMARY:
==18311== definitely lost: 32 bytes in 2 blocks
==18311== indirectly lost: 0 bytes in 0 blocks
==18311== possibly lost: 0 bytes in 0 blocks
==18311== still reachable: 0 bytes in 0 blocks
==18311== suppressed: 0 bytes in 0 blocks
爲什麼我不能釋放這兩個變量?
編輯
someList *something; *something_else;
使用的結構有char *
類型的兩個字段和字段someList *next
。 後來有很多代碼。一些線程將使用這兩個變量來添加/編輯/刪除對象。
something -> object 1 -> ... -> object n
something_else -> object 1 -> ... -> object m
凡->
意味着something->next = object 1
,和object k
是someList *
所有實例。
在應用程序結束時,我釋放了每個object k
元素的每個字段。然後,在最後一部分:
free(something);
free(something_else);
這是可能的,我忘了釋放一個對象的字段。這會導致我在這裏的行爲嗎?
我希望現在更清楚。
是你執行它們的代碼的一部分嗎?主函數的早期返回可能會跳過它。 – CodesInChaos 2011-01-22 13:32:25
需要查看代碼。 – SoapBox 2011-01-22 13:32:53