VS2010稽查内存泄露
VS2010检查内存泄露
VS2010中的C++程序内存泄露检测
MFC程序是支持内存检测的。对于非MFC程序而言,CRT有一套内存泄露的函数,最常用的是 _CrtDumpMemoryLeaks();如下所示:
#include <crtdbg.h> int main() { int *pInt = new int(); char *pChar = new char(); double *pDouble = new double(); // position 1 _CrtDumpMemoryLeaks(); return 0; }运行之后,结果如图1所示:
可以看到,在第71次({71})分配内存时发生了泄露,略有不足的是,没有显示出是哪一行导致的内存泄露。将#include <crtdbg.h>(<crtdgb.h>必须被包含)改为如下:
#ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__) #else #define DEBUG_CLIENTBLOCK #endif #define _CRTDBG_MAP_ALLOC #include <crtdbg.h> #ifdef _DEBUG #define new DEBUG_CLIENTBLOCK #endif运行之后,结果如图2所示:
可以看到是因为main.cpp的第16行导致了4 bytes的内存泄露,第17行...第18行...
但是这段代码仍然存在一些问题,例如_CrtDumpMemoryLeaks()放在position 1时,虽然接下来使用delete释放new分配的内存,但是运行后结果与图2仍然相同。如下所示:
int main() { int *pInt = new int(); char *pChar = new char(); double *pDouble = new double(); // position 1 _CrtDumpMemoryLeaks(); delete pInt; delete pChar; delete pDouble; //position 2 // _CrtDumpMemoryLeaks(); return 0; }最好的办法是将_CrtDumpMemoryLeaks()放置在函数的出口处(如position 2处)。
这个方法对于C++中由程序员自定义的类对象创建和销毁是不够正确的,如下所示:
class Apple { public: Apple(){ ptr = new char(); } ~Apple(){ delete ptr; } char *ptr; }; int main() { int *pInt = new int(); char *pChar = new char(); double *pDouble = new double(); // position 1 // _CrtDumpMemoryLeaks(); Apple app; delete pInt; delete pChar; delete pDouble; //position 2 _CrtDumpMemoryLeaks(); return 0; }运行结果如图3所示:
其中第17行的代码为
Apple(){ ptr = new double(); }可以看到即使类Apple中定义了析构函数释放内存,但是position 2处的_CrtDumpMemoryLeaks();仍然给出了内存泄露的信息。
改进的方法就是在分配内存之前使用_CrtSetDbgFlag()函数(_CrtSetDbgFlag()可以在程序退出时调用_CrtDumpMemoryLeaks()),如下所示:
int _CrtSetDbgFlag( int newFlag );newFlag的参数如下所示(详见_CrtSetDbgFlag函数介绍和MSDN的_CrtSetDbgFlag函数):
Bit field | Default | Description |
_CRTDBG_ALLOC- _MEM_DF |
ON | ON: Enable debug heap allocations and use of memory block type identifiers, such as_CLIENT_BLOCK. OFF: Add new allocations to heap’s linked list, but set block type to _IGNORE_BLOCK. |
_CRTDBG_CHECK- _ALWAYS_DF |
OFF | ON: Call
_CrtCheckMemory at every allocation and deallocation request. OFF: _CrtCheckMemory must be called explicitly. |
_CRTDBG_CHECK- _CRT_DF |
OFF | ON: Include _CRT_BLOCK types in leak detection and memory state difference operations. OFF: Memory used internally by the run-time library is ignored by these operations. |
_CRTDBG_DELAY- _FREE_MEM_DF |
OFF | ON: Keep freed memory blocks in the heap’s linked list, assign them the_FREE_BLOCK type, and fill them with the byte value 0xDD. OFF: Do not keep freed blocks in the heap’s linked list. |
_CRTDBG_LEAK- _CHECK_DF |
OFF | ON: Perform automatic leak checking at program exit via a call to_CrtDumpMemoryLeaks and generate an error report if the application failed to
free all the memory it allocated. OFF: Do not automatically perform leak checking at program exit. |
int main() { _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); int *pInt = new int(); char *pChar = new char(); double *pDouble = new double(); // position 1 // _CrtDumpMemoryLeaks(); Apple app; // delete pInt; delete pChar; delete pDouble; //position 2 // _CrtDumpMemoryLeaks(); return 0; }运行结果如图4所示:
正确的指出了pInt指向的内存没有释放,类Apple的正确行为不再显示。
如果将Apple类的析构函数变化一下
~Apple(){ delete ptr; }变为:
~Apple(){ }运行结果如图5所示:
可以看到_CrtSetDbgFlag()函数正确地指出了Apple类对象的内存没有释放。
参考的文章有内存管理:_CrtDumpMemoryLeaks和_CrtSetBreakAlloc和使用_CrtSetDbgFlag检测内存泄露和检测C++的内存泄漏(win7 64 vs2010)和atexit_百度百科。
版权声明:本文为博主原创文章,未经博主允许不得转载。