如何在VC ++中查找内存泄漏 [英] How to find memory leaks in VC++

查看:73
本文介绍了如何在VC ++中查找内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



找出vc ++内存泄漏的所有方法是什么?



问候,

Ranjith

解决方案

如果您使用C ++和MFC,则有一个内置泄漏检测器。在IDE中运行程序时,它将在程序终止时在输出窗口中打印出泄漏检测报告。默认情况下,它会告诉您有关泄漏的信息,但不会报告泄漏源自何处。要启用文件和行信息,在您认为可能发生泄漏的每个文件的顶部,在所有#include和#import语句之后但在任何静态变量,函数或方法之前,添加以下内容:



 #ifdef _DEBUG 
#define new DEBUG_NEW
#endif





这样可以在泄漏报告中检测文件和行(仅限调试版本)。



如果你不做这样,您将在输出窗口中看到如下内容:



检测到内存泄漏! 
转储对象 - > ;
{18}正常块位于0x00781248,64字节长。
数据:< > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
对象转储完成。





如果你添加序言,你会得到:



检测到内存泄漏! 
转储对象 - > ;
D:\ workarea \ LeakyProject \yadayada\yadayada.cpp(62):{18}
正常块位于0x00781248,64字节长。
数据:< > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
对象转储完成。





如果您不使用MFC,MSDN将介绍如何执行上述此处 [ ^ ]手动。恕我直言,这需要比其他答案少得多的设置。



此外,如果您使用MFC,那么您可以在应用程序运行时实时监控内存分配,使用以下技术。请注意,这仅适用于调试版本。



正如其他人发布的那样,有更复杂的方法来跟踪泄漏。在开发过程中进行泄漏检测时,您应该使用最容易使用的机制。



 #ifdef _DEBUG 
AFX_ALLOC_HOOK _OldHook = NULL;
BOOL AFXAPI AllocHook(size_t nSize,BOOL bObject,LONG lRequestNumber)
{
if(_OldHook)
return _OldHook(nSize,bObject,lRequestNumber);
返回FALSE;
}

void SetHook()
{
_OldHook = AfxSetAllocHook(AllocHook);
}


CMainApp :: CMainApp()
{
#ifdef _DEBUG
SetHook();
#endif
}

#endif





然后运行代码并查看泄露的内存通知,您还将看到创建泄漏的请求编号。再次运行应用程序时,您可以查看此请求号以查看导致泄漏的代码。


关于内存lek finder的代码项目中最好的C ++文章之一是

C ++ Memory Leak Finder



这是一个很好的。请参阅


http://pages.cs.wisc.edu/ ~tlabonne / memleak.html [ ^ ]

C ++中的内存泄漏检测 [ ^ ]

Hi,
what are all the ways to find out the memory leak in vc++?

Regards,
Ranjith

解决方案

If you are using C++ with MFC, there is a builtin leak detector. When you run a program in the IDE, it will print out a leak detection report in the output window when your program terminates. By default, it tells you about the leaks but does not report where the leak originated. To enable file and line information, at the top of every file where you think a leak might occur, after all "#include" and "#import" statements but before any static variables, functions or methods, add the following:

#ifdef _DEBUG
#define new DEBUG_NEW
#endif



This will enable detection of file and line in the leak report (debug builds only).

If you don''t do this, you will see something in the output window that looks like:

Detected memory leaks!
Dumping objects - > ;
{18} normal block at 0x00781248, 64 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.



If you add the preamble, you will get:

Detected memory leaks!
Dumping objects - > ;
D:\workarea\LeakyProject\yadayada\yadayada.cpp(62) : {18}
normal block at 0x00781248, 64 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.



If you do not use MFC, MSDN describes how to do the above here[^] manually. IMHO this requires much less setup than the other answers.

Additionally, if you use MFC then you can monitor memory allocations, in real-time while your application runs, using the following technique. Note that this is only available in debug builds.

As others have posted, there are far more sophisticated methods to track leaks. When it comes to leak detection during development then you should use the mechanism that is easiest to use.

#ifdef _DEBUG
AFX_ALLOC_HOOK _OldHook = NULL;
BOOL AFXAPI AllocHook(size_t nSize,BOOL bObject,LONG lRequestNumber)
{
	if (_OldHook)
		return _OldHook(nSize,bObject,lRequestNumber);
	return FALSE;
}

void SetHook()
{
	_OldHook = AfxSetAllocHook(AllocHook);
}


CMainApp::CMainApp()
{
#ifdef _DEBUG
     SetHook();
#endif
}

#endif



Then after you run your code and see the leaked memory notice that you will also see the request number that created the leak. When you run your application again you can break in at this request number to see the code that is causing the leak.


One of the best C++ article in code project about memory lek finder is
C++ Memory Leak Finder

This is a good one. please refer


http://pages.cs.wisc.edu/~tlabonne/memleak.html[^]
Memory leak detection in C++[^]


这篇关于如何在VC ++中查找内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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