C++ - 使用 _CrtDumpMemoryLeaks() 进行内存泄漏测试 - 不输出行号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3202520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ - Memory leak testing with _CrtDumpMemoryLeaks() - Does not output line numbers
提问by John
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.
我正在 Visual Studio 2010 中使用 SDL 开发一个游戏。我遇到了_CrtDumpMemoryLeaks()
宏,并想试一试。调用_CrtDumpMemoryLeaks()
确实将内存泄漏打印到输出窗口,但它不显示发生的位置。
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').
我在Memory Leak Detection Enable阅读了 MSDN 文章,它解释了如果我定义_CRTDBG_MAP_ALLOC
它应该输出违规语句的行号。在我的情况下不会发生这种情况。(但是,如果我直接使用 malloc() —— 而不是使用“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.
如果_CrtDumpMemoryLeaks()
在使用“new”进行分配时无法输出行号,那么对于实现类似行为的其他方法的建议表示赞赏。
采纳答案by CB Bailey
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.
当您定义 _DEBUG 并包含时,<crtdbg.h>
您会得到一个重载operator new
,它带有额外的参数,您可以使用这些参数来指定放置new
表达式中的文件和行号。
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'.
虽然您确实看到有人定义了一个宏new
来完全隐藏此表单客户端代码,但我个人并不推荐它,因为它破坏了任何已经有意使用新布局的内容,并且您必须确保使用新布局的任何标题(例如许多标准标题)包含在任何标头重新定义之前new
。这可以很容易地让new
in 头文件的一些内联使用在不被“调整”的情况下溜走。
回答by Tafsen
That's an old version of Visual Leak Detector.
这是旧版本的 Visual Leak Detector。
Try this: http://vld.codeplex.com/
试试这个:http: //vld.codeplex.com/
回答by ULysses
You might need these defines after your includes
在包含之后,您可能需要这些定义
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
回答by DumbCoder
Check out the piece of code.
查看这段代码。
Overloading operator new and operator delete to log all memory allocations and deallocations
重载 operator new 和 operator delete 以记录所有内存分配和解除分配
I identified my memory leaks using this method.
我使用这种方法确定了我的内存泄漏。
回答by Special Sauce
The accepted answer by Charles Bailey requires that you change your source code and this is not necessary. If you are using new
and delete
(or the array versions), all you need to do is place this code snippet in the stdafx.h
file of each of your projects (including any static or dynamic library dependencies), and it will then give a source file and line number attached to each leaked memory object:
Charles Bailey 接受的答案要求您更改源代码,而这不是必需的。如果您使用new
和delete
(或数组版本),您需要做的就是将此代码片段放在stdafx.h
每个项目的文件中(包括任何静态或动态库依赖项),然后它会给出一个源文件和行附加到每个泄漏内存对象的数字:
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
This comes straight from Microsoft's webpageon the matter.
这直接来自微软关于此事的网页。