在工作中,我用C編寫了一個相當複雜的軟件,我經常使用valgrind來測試它。到目前爲止,該程序完美地工作,沒有內存泄漏或數組邊界違規,並且在valgrind報告中,'frees'的數量與'malloc'的數量相匹配 - 非常好。令我感到困擾的是它報告了數千個釋放和malloc。而且我知道一個事實,我沒有超過50-60。我知道當我的程序調用'fopen'時,這個調用被valgrind計算到malloc的數量,同樣'fclose'被計算爲'frees'的數量。但在我的情況下,這仍然不能解釋我看到的內存被混合和釋放的次數。我仔細搜尋了代碼,尋找罪魁禍首,但我什麼都沒有。我不能在這裏發佈任何代碼,原因很明顯,但我只想知道,我錯過了什麼? valgrind是否還有其他C操作計入malloc和free的數量?valgrind將所有C操作都視爲「malloc」和「free」?
這是我的valgrind報告。正如你所看到的,從這個角度看,一切都很好。
Memcheck, a memory error detector
Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
Command: ./Codec
Parent PID: 3526
HEAP SUMMARY:
in use at exit: 0 bytes in 0 blocks
total heap usage: 2,407 allocs, 2,407 frees, 28,877,748 bytes allocated
All heap blocks were freed -- no leaks are possible
For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
對於(int i = 0; i!= 10000; ++ i){free(malloc(1)); }'? –
如果您調用C庫例程或與任何庫鏈接並調用它們,它們可以調用malloc()和free()。 –
你使用任何庫嗎?通常他們會在幕後分配和釋放內存 – Pankrates