- 論壇徽章:
- 0
|
今天調(diào)試一段代碼的時候,無意間發(fā)現(xiàn)內(nèi)存泄漏,top命令查看到的進(jìn)程內(nèi)存持續(xù)增長。檢查了半天的malloc和free,實(shí)在是沒有找到問題。沒辦法,在網(wǎng)上查看了寫帖子,知道有Valgrind這個東東,于是下下來,試了下,結(jié)果,就執(zhí)行了一次便發(fā)現(xiàn)了程序的問題:
使用參數(shù)valgrind --tool=memcheck --leak-check=yes ./hsvr_smp,
==16790==
==16790== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 16 from 2)
==16790== malloc/free: in use at exit: 1003488 bytes in 241 blocks.
==16790== malloc/free: 333 allocs, 92 frees, 1366168 bytes allocated.
==16790== For counts of detected errors, rerun with: -v
==16790== searching for pointers to 241 not-freed blocks.
==16790== checked 10218852 bytes.
==16790==
==16790== 3872 bytes in 44 blocks are possibly lost in loss record 18 of 21
==16790== at 0x1B904984: malloc (vg_replace_malloc.c:131)
==16790== by 0x80527C4: my_malloc (in /home/nari/exe/hsvr_smp3)
==16790== by 0x80686C1: mysql_store_result (in /home/nari/exe/hsvr_smp)
==16790== by 0x804B173: HDBChecktbl (hmysql.c:398)
==16790==
==16790== LEAK SUMMARY:
==16790== definitely lost: 0 bytes in 0 blocks.
==16790== possibly lost: 3872 bytes in 44 blocks.
==16790== still reachable: 999616 bytes in 197 blocks.
==16790== suppressed: 0 bytes in 0 blocks.
==16790== Reachable blocks (those to which a pointer was found) are not shown.
==16790== To see them, rerun with: --show-reachable=yes
看到上面提示的函數(shù)棧,找到HDBChecktbl 函數(shù)內(nèi)的 mysql_store_result 函數(shù),原來
這段代碼中調(diào)用了mysql_store_result卻沒有調(diào)用mysql_free_result。難怪自己沒有找到malloc和free方面的錯誤。
修改相關(guān)程序再執(zhí)行 ,得到下面的結(jié)果:
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 16 from 2)
==16892== malloc/free: in use at exit: 278896 bytes in 65 blocks.
==16892== malloc/free: 165 allocs, 100 frees, 675016 bytes allocated.
==16892== For counts of detected errors, rerun with: -v
==16892== searching for pointers to 65 not-freed blocks.
==16892== checked 10203892 bytes.
==16892==
==16892== LEAK SUMMARY:
==16892== definitely lost: 0 bytes in 0 blocks.
==16892== possibly lost: 0 bytes in 0 blocks.
==16892== still reachable: 278896 bytes in 65 blocks.
==16892== suppressed: 0 bytes in 0 blocks.
==16892== Reachable blocks (those to which a pointer was found) are not shown.
==16892== To see them, rerun with: --show-reachable=yes
由此看到,valgrind 可以很方便的指導(dǎo)程序員找到錯誤的地方。
本文來自ChinaUnix博客,如果查看原文請點(diǎn):http://blog.chinaunix.net/u2/87101/showart_2098251.html |
|