您的位置:首页 > 其它

Backtraces引起的内存泄露

2010-08-10 18:25 417 查看
最近在找服务器的内存泄露,在debug的地方经常发现内存泄露,查看文档以后发现是Backtraces引起的内存泄露

下面是使用Backtraces的方法

#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>

/* Obtain a backtrace and print it to stdout. */
void
print_trace (void)
{
void *array[10];
size_t size;
char **strings;
size_t i;

size = backtrace (array, 10);
strings = backtrace_symbols (array, size);

printf ("Obtained %zd stack frames./n", size);

for (i = 0; i < size; i++)
printf ("%s/n", strings[i]);

free (strings);
}

/* A dummy function to make the backtrace more interesting. */
void
dummy_function (void)
{
print_trace ();
}

int
main (void)
{
dummy_function ();
return 0;
}


务必要对backtrace_symbols的返回值strings调用free

切记
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: