环境: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
http://blog.csdn.net/herx1/article/details/2199438
推荐使用Wiz,它是一款基于云存储的笔记软件,很快很好用,
使用我的邀请注册可获VIP体验: http://www.wiz.cn/i/d9c623b8
在VC2005 + WTL 8.1环境下,引入crtdbg.h以查找内存泄漏时,导致创建ActiveX窗口报错。问题源于_malloca分配的内存被_freea尝试释放,但_freea寻找的安全cookie缺失。解决方法是理解这是由于malloc.h和crtdbg.h包含顺序引发的bug,并非实际问题,因为发布版不会包含crtdbg.h。此问题在ATL ActiveX Container的调试中常见,表现为_freea断言失败。可以参考相关文章了解更多关于security cookie的研究。
4421

被折叠的 条评论
为什么被折叠?



