您的位置:首页 > 其它

定位问题:*** glibc detected *** : double free or corruption (!prev): 0x09b077d8

2015-07-15 15:33 531 查看
定位free/malloc的位置和参数,可以在对应的.cpp/.h文件中添加:


C代码



#include <stdio.h>

#include <stdlib.h>







void *

debug_malloc(size_t size, const char *file, int line, const char *func)

{

void *p;



p = malloc(size);

printf("%s:%d:%s:malloc(%ld): p=0x%lx\n",

file, line, func, size, (unsigned long)p);

return p;

}



#define malloc(s) debug_malloc(s, __FILE__, __LINE__, __func__)

#define free(p) do { \

printf("%s:%d:%s:free(0x%lx)\n", __FILE__, __LINE__, \

__func__, (unsigned long)p); \

free(p); \

} while (0)



int

main(int argc, char *argv[])

{

char *p;

p = malloc(1024);

free(p);

return 0;

}


延伸一下,如果想在不改动原来代码的情况下跳过这个错误,则可以将上述代码再重新改写一下:
#define malloc(s) debug_malloc(s, __FILE__, __LINE__, __func__)

#define free(p) do { \

printf("%s:%d:%s:free(0x%lx)\n", __FILE__, __LINE__, \

__func__, (unsigned long)p); \
if (p) { \

free(p); \
p = NULL; \
} \

} while (0)

from: http://blog.csdn.net/cuiyifang/article/details/8237569
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: