C++ - 使用 _CrtDumpMemoryLeaks() 进行内存泄漏测试 - 不输出行号
我正在 Visual Studio 2010 中使用 SDL 开发一款游戏.我遇到了 _CrtDumpMemoryLeaks()
宏,并想试一试.调用 _CrtDumpMemoryLeaks()
确实将内存泄漏打印到输出窗口,但它没有显示发生的位置.
我在 阅读了 MSDN 文章内存泄漏检测启用 ,它解释了如果我定义 _CRTDBG_MAP_ALLOC
它应该输出违规语句的行号.在我的情况下不会发生这种情况.(但是,如果我直接使用 malloc() ―― 而不是使用new",我能够让它工作).
代码:
#define _CRTDBG_MAP_ALLOC#include #include int main(int argc, char *argv[]) {int *var = new int(5);_CrtDumpMemoryLeaks();返回0;}
输出如下:<代码><预>检测到内存泄漏!转储对象 ->{58} 位于 0x007D1510 的普通块,4 字节长.数据:<> 05 00 00 00对象转储完成.
如果 _CrtDumpMemoryLeaks()
在使用new"分配时无法输出行号,那么我们感谢提供其他实现类似行为的方法的建议.
当您定义 _DEBUG 并包含 <crtdbg.h>
时,您会得到一个重载的 operator new
接受额外的参数,您可以使用这些参数来指定放置 new
表达式中的文件和行号.
例如
int* p = new (_NORMAL_BLOCK, __FILE__, __LINE__) int(5);
您可以将其包装在条件定义的宏中,例如
#ifdef _DEBUG#define DEBUG_NEW_PLACEMENT (_NORMAL_BLOCK, __FILE__, __LINE__)#别的#define DEBUG_NEW_PLACEMENT#万一int* p = new DEBUG_NEW_PLACEMENT int(5);
虽然您确实看到人们定义了一个宏 new
来完全隐藏此表单客户端代码,但我个人并不推荐它,因为它破坏了任何已经有意使用 Placement new 的内容,您必须确保任何在重新定义 new
的任何标头之前包含使用放置 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'.
相关文章