使用_CRTDBG_MAP_ALLOC宏与_malloca/_freea不兼容

来源:互联网 发布:死亡女神 漫威 知乎 编辑:程序博客网 时间:2024/06/11 21:53

环境:VC2005  + WTL 8.1

 

为了定位内存漏洞,在头文件中包含了crtdbg.h,

 

#if defined(_DEBUG)#define _CRTDBG_MAP_ALLOC#include <stdlib.h>#include <crtdbg.h>#endif // defined(_DEBUG)


结果造成创建包含有ActiveX的窗口时报错,

问题来自于~CAutoStackPtr这个析构函数,它试图调用_freea释放由_malloca申请的内存,

但是  _malloca已经被定义成了_malloc_dbg,而_freea却要寻找一个不存在的security cookie。

 

解决:

这个问题不用解决,反正release版不会带crtdbg.h的。

 

PS:这其实是malloc.h与crtdbg.h顺序导致的 bug,在 VisualStudio 2008 中开发 ATL ActiveX Container 时,调试程序常常会遇到 Debug Assertion Failed, "Corrupted pointer passed to _freea" 的对话框。出错的位置发生在 malloc.h 第252行。直接原因是 ATL 深层代码 ~CAutoStackPtr() 析构函数试图调用 _freea 来释放 _malloca 分配的内存块而导致的错误。根本原因是调试时监控内存泄露的程序与_malloca/_freea相冲突:

_malloca/_freea相冲突:#ifdef _DEBUG#define   _CRTDBG_MAP_ALLOC#include <stdlib.h>#include <crtdbg.h>#endif//_DEBUG

改为

#ifdef _DEBUG#define   _CRTDBG_MAP_ALLOC#include <stdlib.h>#include <malloc.h>#include <crtdbg.h>#endif//_DEBUG

  

参考:

ATL ActiveX control hosting memory leak, _freea assertion failure and more

security cookie

http://blog.csdn.net/herx1/article/details/2199438

security cookie在栈保护上的研究

 

 

推荐使用Wiz,它是一款基于云存储的笔记软件,很快很好用,

使用我的邀请注册可获VIP体验: http://www.wiz.cn/i/d9c623b8

原创粉丝点击