使用CRT内存泄漏检测时编译错误 [英] Compile errors when I using CRT Memory Leak Detection

查看:140
本文介绍了使用CRT内存泄漏检测时编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要检测内存泄漏,重新定义新关键字。
如果我使用[类型1],可以。但编译错误发生,如果我取消注释[类型2]。
有没有办法使用这两种类型的新的?

To detect memory leak, new keyword is re-defined. It's OK if I use [Type 1]. But compile error is occured if I uncomment [Type 2]. Is there any way to use both type of new?

#include <crtdbg.h>

#define new new(_CLIENT_BLOCK, __FILE__, __LINE__)

struct Foo
{
 int m_N;
 Foo() : m_N( 0 ) {}
};

int main( int argc, char* argv[] )
{
 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
 _CrtSetReportMode(_CRT_WARN , _CRTDBG_MODE_FILE);
 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);

 int* pI = new int( 1 ); 
 delete pI;

 Foo* pFoo = new Foo;    // [Type 1]
 //Foo* pFoo2 = new (pFoo) Foo(); // [Type 2]

 return 0;
}


推荐答案

新扩展为:

Foo* pFoo2 = new(_CLIENT_BLOCK, __FILE__, __LINE__) (pFoo) Foo();

这显然是无效语法。

MSFT工程师确认


#define of new与刊登位置新

#define of new is not compatible with placement new

不相容,因此您不能在 crtdbg.h

so you can't use it with crtdbg.h.

这篇关于使用CRT内存泄漏检测时编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆