Linux ring3内存泄漏检测工具使用
一、内存泄漏测试代码
/* gcc backtrace_symbols.c -o backtrace_symbols -rdynamic */
#include <stdio.h>
#include <execinfo.h>
#define STORE_STACK_MAX_SIZE 10
static int print_stack(void)
{
int size = 0, i = 0;
void *buffer[STORE_STACK_MAX_SIZE];
char **string = NULL;
size = backtrace(buffer, STORE_STACK_MAX_SIZE);
string = backtrace_symbols(buffer, size);
if(string == NULL) {
printf("%s: backtrace symble is NULL\n", __func__);
return -1;
}
for(i = 0; i < size; i++) {
printf("%s\n", string[i]);
}
// free(string);
return 0;
}
int main(void)
{
printf("%s: print stack addr = 0x%x\n", __func__, print_stack);
print_stack();
return 0;
}
上述代码在使用libc库的栈回溯功能的时候,没有对string指向的内存做释放,这就造成了内存泄漏。
二、内存泄漏检测工具valgrind使用
sudo apt-get install valgrind
curtis@curtis-virtual-machine:~/Desktop/work/backstrace$ valgrind --leak-check=full ./a.out
==8420== Memcheck, a memory error detector
==8420== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8420== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==8420== Command: ./a.out
==8420==
main: print stack addr = 0x10896a
./a.out(+0x9a8) [0x1089a8]
./a.out(main+0x28) [0x108a5d]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) [0x4e5fbf7]
./a.out(_start+0x2a) [0x10888a]
==8420==
==8420== HEAP SUMMARY:
==8420== in use at exit: 283 bytes in 1 blocks
==8420== total heap usage: 7 allocs, 6 frees, 3,001 bytes allocated
==8420==
==8420== 283 bytes in 1 blocks are definitely lost in loss record 1 of 1
==8420== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8420== by 0x4F6F1F3: backtrace_symbols (backtracesyms.c:69)
==8420== by 0x1089BB: print_stack (in /home/curtis/Desktop/work/backstrace/a.out)
==8420== by 0x108A5C: main (in /home/curtis/Desktop/work/backstrace/a.out)
==8420==
==8420== LEAK SUMMARY:
==8420== definitely lost: 283 bytes in 1 blocks
==8420== indirectly lost: 0 bytes in 0 blocks
==8420== possibly lost: 0 bytes in 0 blocks
==8420== still reachable: 0 bytes in 0 blocks
==8420== suppressed: 0 bytes in 0 blocks
==8420==
==8420== For counts of detected and suppressed errors, rerun with: -v
==8420== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
从上面的信息可以看出内存泄漏产生的整个调用流程,main->print_stack->backtrace_symbos->malloc,最终没有释放backtrace_symbos申请的内存,导致283bytes的内存泄漏。
内存释放之后使用工具扫描:
curtis@curtis-virtual-machine:~/Desktop/work/backstrace$ valgrind --leak-check=full ./a.out
==12664== Memcheck, a memory error detector
==12664== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==12664== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==12664== Command: ./a.out
==12664==
main: print stack addr = 0x1089aa
./a.out(+0x9e8) [0x1089e8]
./a.out(main+0x28) [0x108aa9]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) [0x4e5fbf7]
./a.out(_start+0x2a) [0x1088ca]
==12664==
==12664== HEAP SUMMARY:
==12664== in use at exit: 0 bytes in 0 blocks
==12664== total heap usage: 7 allocs, 7 frees, 3,001 bytes allocated
==12664==
==12664== All heap blocks were freed -- no leaks are possible
==12664==
==12664== For counts of detected and suppressed errors, rerun with: -v
==12664== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)