Ubuntu 安装valgrind:
#> sudo apt-get install valgrind
CentOS
[root@demo debug]# yum install valgrind
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors。。。。
[root@demo debug]# valgrind --tool=memcheck --leak-check=yes ./vs
注意在编译程序的时候加上-g选项,打印错误信息的时候会给出行号。
==21735==
==21735== HEAP SUMMARY:
==21735== in use at exit: 13,230,426 bytes in 8,400 blocks
==21735== total heap usage: 559,825 allocs, 551,425 frees, 149,927,393 bytes allocated
==21735==
==21735== Thread 1:
==21735== 1 bytes in 1 blocks are definitely lost in loss record 5 of 149
==21735== at 0x4A06A2E: malloc (vg_replace_malloc.c:270)
==21735== by 0x4C2B469: os_startup (libos.c:676)
==21735== by 0x406047: main (vs.c:1545)
==21735==
==21735== 1 bytes in 1 blocks are definitely lost in loss record 6 of 149
==21735== at 0x4A06A2E: malloc (vg_replace_malloc.c:270)
==21735== by 0x4C2B50C: os_startup (libos.c:689)
==21735== by 0x406047: main (vs.c:1545)
==21735==
==21735== 41 bytes in 1 blocks are definitely lost in loss record 62 of 149
==21735== at 0x4A07192: operator new[](unsigned long) (vg_replace_malloc.c:363)
==21735== by 0x50E3BFD: strDup(char const*) (in /home/EasyLive/1.0beta-fec/debug/librtsp.so)
==21735== by 0x50B6EBB: RTSPClient::handleResponseBytes(int) (in /home/EasyLive/1.0beta-fec/debug/librtsp.so)
==21735== by 0x50B7206: RTSPClient::incomingDataHandler1() (in /home/EasyLive/1.0beta-fec/debug/librtsp.so)
==21735== by 0x50E1A80: BasicTaskScheduler::SingleStep(unsigned int) (in /home/EasyLive/1.0beta-fec/debug/librtsp.so)
==21735== by 0x50E31A3: BasicTaskScheduler0::doEventLoop(char volatile*) (in /home/EasyLive/1.0beta-fec/debug/librtsp.so)
==21735== by 0x50A4D80: rtsp_task_thread_func(void*) (rtsp.cpp:446)
==21735== by 0x3A7EA07AA0: start_thread (in /lib64/libpthread-2.12.so)
==21735== by 0x3A7DEE893C: clone (in /lib64/libc-2.12.so)
==21735==
==21735== 576 bytes in 1 blocks are possibly lost in loss record 124 of 149
==21735== at 0x4A057BB: calloc (vg_replace_malloc.c:593)
==21735== by 0x3A7DA119A2: _dl_allocate_tls (in /lib64/ld-2.12.so)
==21735== by 0x3A7EA072CC: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.12.so)
==21735== by 0x4C30192: os_thread_create (libos.c:2520)
==21735== by 0x4C2B565: os_startup (libos.c:695)
==21735== by 0x406047: main (vs.c:1545)
==21735==
==21735== 576 bytes in 1 blocks are possibly lost in loss record 125 of 149
==21735== at 0x4A057BB: calloc (vg_replace_malloc.c:593)
==21735== by 0x3A7DA119A2: _dl_allocate_tls (in /lib64/ld-2.12.so)
==21735== by 0x3A7EA072CC: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.12.so)
==21735== by 0x4C30192: os_thread_create (libos.c:2520)
==21735== by 0x4C2B5CF: os_startup (libos.c:700)
==21735== by 0x406047: main (vs.c:1545)
==21735==
==21735== 576 bytes in 1 blocks are possibly lost in loss record 126 of 149
==21735== at 0x4A057BB: calloc (vg_replace_malloc.c:593)
==21735== by 0x3A7DA119A2: _dl_allocate_tls (in /lib64/ld-2.12.so)
==21735== by 0x3A7EA072CC: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.12.so)
==21735== by 0x4C30192: os_thread_create (libos.c:2520)
==21735== by 0x4061A3: main (vs.c:1568)
==21735==
==21735== 576 bytes in 1 blocks are possibly lost in loss record 127 of 149
==21735== at 0x4A057BB: calloc (vg_replace_malloc.c:593)
==21735== by 0x3A7DA119A2: _dl_allocate_tls (in /lib64/ld-2.12.so)
==21735== by 0x3A7EA072CC: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.12.so)
==21735== by 0x4C30192: os_thread_create (libos.c:2520)
==21735== by 0x406264: main (vs.c:1577)
==21735==
==21735== 576 bytes in 1 blocks are possibly lost in loss record 128 of 149
==21735== at 0x4A057BB: calloc (vg_replace_malloc.c:593)
==21735== by 0x3A7DA119A2: _dl_allocate_tls (in /lib64/ld-2.12.so)
==21735== by 0x3A7EA072CC: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.12.so)
==21735== by 0x4C30192: os_thread_create (libos.c:2520)
==21735== by 0x40633B: main (vs.c:1589)
==21735==
==21735== 576 bytes in 1 blocks are possibly lost in loss record 129 of 149
==21735== at 0x4A057BB: calloc (vg_replace_malloc.c:593)
==21735== by 0x3A7DA119A2: _dl_allocate_tls (in /lib64/ld-2.12.so)
==21735== by 0x3A7EA072CC: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.12.so)
==21735== by 0x4C30192: os_thread_create (libos.c:2520)
==21735== by 0x406442: main (vs.c:1606)
==21735==
==21735== 576 bytes in 1 blocks are possibly lost in loss record 130 of 149
==21735== at 0x4A057BB: calloc (vg_replace_malloc.c:593)
==21735== by 0x3A7DA119A2: _dl_allocate_tls (in /lib64/ld-2.12.so)
==21735== by 0x3A7EA072CC: pthread_create@@GLIBC_2.2.5 (in /lib64/libpthread-2.12.so)
==21735== by 0x4C30192: os_thread_create (libos.c:2520)
==21735== by 0x50A4E67: rtsp_init() (rtsp.cpp:463)
==21735== by 0x50A4FDF: rtsp_client_play (rtsp.cpp:556)
==21735== by 0x4052CD: on_thread_rtsp_play (vs.c:1225)
==21735== by 0x3A7EA07AA0: start_thread (in /lib64/libpthread-2.12.so)
==21735== by 0x3A7DEE893C: clone (in /lib64/libc-2.12.so)
==21735==
==21735== 392,352 (362,208 direct, 30,144 indirect) bytes in 7,546 blocks are definitely lost in loss record 148 of 149
==21735== at 0x4A07192: operator new[](unsigned long) (vg_replace_malloc.c:363)
==21735== by 0x5318499: fec_encode (fec.cpp:229)
==21735== by 0x404027: on_thread_fec (vs.c:779)
==21735== by 0x3A7EA07AA0: start_thread (in /lib64/libpthread-2.12.so)
==21735== by 0x3A7DEE893C: clone (in /lib64/libc-2.12.so)
==21735==
==21735== LEAK SUMMARY:
==21735== definitely lost: 362,251 bytes in 7,549 blocks
==21735== indirectly lost: 30,144 bytes in 628 blocks
==21735== possibly lost: 4,032 bytes in 7 blocks
==21735== still reachable: 12,833,999 bytes in 216 blocks
==21735== suppressed: 0 bytes in 0 blocks
==21735== Reachable blocks (those to which a pointer was found) are not shown.
==21735== To see them, rerun with: --leak-check=full --show-reachable=yes
==21735==
==21735== For counts of detected and suppressed errors, rerun with: -v
==21735== Use --track-origins=yes to see where uninitialised values come from
==21735== ERROR SUMMARY: 43019 errors from 14 contexts (suppressed: 6 from 6)
Killed
[root@demo debug]#