用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

IT开发者    -  云代码空间

—— 20141227

在MFC中报告内存泄露

2014-04-05|1949阅||

摘要:如果你想在MFC应用程序中监测内存泄露,你可以使用宏DEBUG_NEW来重定义new运算符,这是new运算符的一个修改版本,可以记录其分配内存的文件名及行数。在Release版中构建的DEBUG_NEW会解析成原始的new运算符。 MFC向导产生的源代码中在#include后米

如果你想在MFC应用程序中监测内存泄露,你可以使用宏DEBUG_NEW来重定义new运算符,这是new运算符的一个修改版本,可以记录其分配内存的文件名及行数。在Release版中构建的DEBUG_NEW会解析成原始的new运算符。

MFC向导产生的源代码中在#include后米娜包含如下预处理指令:

1 #ifdef _DEBUG
2 #define new DEBUG_NEW
3 #endif

上面代码就是如何重定义new运算符的方法。

很多STL头文件和这里定义的new运算符不兼容。如果你在重新定义运算符new之后包含了<map><vector><list><string>等头文件,会有如下错误(以<vector>为例):

01 1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(43) : error C2665: 'operator new': none of the 5 overloads could convert all the argument types
02 1>        c:\program files\microsoft visual studio 9.0\vc\include\new.h(85): could be 'void *operator new(size_t,const std::nothrow_t &) throw()'
03 1>        c:\program files\microsoft visual studio 9.0\vc\include\new.h(93): or       'void *operator new(size_t,void *)'
04 1>        while trying to match the argument list '(const char [70], int)'
05 1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(145) : see reference to function templateinstantiation '_Ty *std::_Allocate<char>(size_t,_Ty *)' being compiled
06 1>        with
07 1>        [
08 1>            _Ty=char
09 1>        ]
10 1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(144) : whilecompiling class templatemember function 'char *std::allocator<_Ty>::allocate(std::allocator<_Ty>::size_type)'
11 1>        with
12 1>        [
13 1>            _Ty=char
14 1>        ]
15 1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\xstring(2216) : see reference to classtemplate instantiation 'std::allocator<_Ty>' being compiled
16 1>        with
17 1>        [
18 1>            _Ty=char
19 1>        ]

解决方法是在包含这些STL文件之后再使用DEBUG_NEW重定义new运算符。


顶 2踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: IT开发者
    • 等级: 资深程序员
    • 积分: 1060
    • 代码: 87 个
    • 文章: 12 篇
    • 随想: 2 条
    • 访问: 3 次
    • 关注

    最新提问

      站长推荐