使用_CRTDBG_MAP_ALLOC宏与_malloca/_freea不兼容

环境: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在栈保护上的研究

 

 

转载于:https://www.cnblogs.com/honker/archive/2011/12/01/3774412.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值