C ++ - 使用_CrtDumpMemoryLeaks()进行内存泄漏测试 - 不输出行号 [英] C++ - Memory leak testing with _CrtDumpMemoryLeaks() - Does not output line numbers

查看:1901
本文介绍了C ++ - 使用_CrtDumpMemoryLeaks()进行内存泄漏测试 - 不输出行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio 2010中使用SDL开发一个游戏。我遇到了 _CrtDumpMemoryLeaks()宏,并且认为我会放弃它。调用 _CrtDumpMemoryLeaks()会将内存泄漏打印到输出窗口,但不会显示它的发生位置。



我已阅读了MSDN文章,网址为:内存泄漏检测使能,它解释说,如果我定义 _CRTDBG_MAP_ALLOC ,它应该输出冒犯语句的行号。这不会发生在我的情况。 (我可以让它工作,如果我直接使用malloc() - 不是使用'新')。



代码:

  #define _CRTDBG_MAP_ALLOC 
#include< crtdbg.h>
#include< stdlib.h>

int main(int argc,char * argv []){
int * var = new int(5);

_CrtDumpMemoryLeaks();

return 0;
}

输出结果如下:

 
检测到内存泄漏!
转储对象 - >
{58}正常块在0x007D1510,4字节长。
数据: > 05 00 00 00
对象转储完成。



如果 _CrtDumpMemoryLeaks()

解决方案

当使用'new'分配时,不能输出行号。你定义_DEBUG并包含< crtdbg.h> 你得到一个重载的运算符new 用于指定表达式中的文件和行号。



例如

  int * p = new(_NORMAL_BLOCK,__FILE__,__LINE__)int(5) 

您可以将其包装在条件定义的宏中,例如

  #ifdef _DEBUG 
#define DEBUG_NEW_PLACEMENT(_NORMAL_BLOCK,__FILE__,__LINE__)
#else
#define DEBUG_NEW_PLACEMENT
#endif

int * p = new DEBUG_NEW_PLACEMENT int(5);

虽然你看到人们定义一个宏 new 完全隐藏此表单客户端代码,我不个人推荐它,因为它打破了任何已经故意使用placement new和你必须确保任何头使用placement新(如许多标准头)包括任何头重定义 new 。这可以使得很容易让头文件中 new 的一些内联使用无需调整。


I'm working on a game with SDL in Visual Studio 2010. I came across the _CrtDumpMemoryLeaks() macro and thought I'd give it a go. Invoking _CrtDumpMemoryLeaks() does print memory leaks to the output window, but it does not show where it happens.

I've read the MSDN article at Memory Leak Detection Enabling , and it explains that if I define _CRTDBG_MAP_ALLOC it should output the line number of the offending statement. This does not happen in my case. (I was however able to get it to work if I use malloc() directly -- not by using 'new').

The code:

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    int *var = new int(5);

    _CrtDumpMemoryLeaks();

    return 0;
}

The output is the following:

Detected memory leaks!
Dumping objects ->
{58} normal block at 0x007D1510, 4 bytes long.
 Data: <    > 05 00 00 00 
Object dump complete.

If _CrtDumpMemoryLeaks() is unable to output line numbers when allocating using 'new' then suggestions for other ways to achieve similar behavior is appreciated.

解决方案

When you define _DEBUG and include <crtdbg.h> you get an overloaded operator new which takes additional parameters which you can use to specify the file and line numbers in placement new expressions.

E.g.

int* p = new (_NORMAL_BLOCK, __FILE__, __LINE__) int(5);

You can wrap this in a conditionally defined macro, e.g.

#ifdef _DEBUG
#define DEBUG_NEW_PLACEMENT (_NORMAL_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_NEW_PLACEMENT
#endif

int* p = new DEBUG_NEW_PLACEMENT int(5);

While you do see people defining a macro new to completely hide this form client code, I do not personally recommend it as it breaks anything already intentionally using placement new and you have to make sure that any headers using placement new (such as many standard headers) are included before any header redefining new. This can make it easy to let some inline uses of new in header files slip through without being 'adjusted'.

这篇关于C ++ - 使用_CrtDumpMemoryLeaks()进行内存泄漏测试 - 不输出行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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