Http://blog.vckbase.com/bruceteen/archive/2004/10/28/1130.aspx This article is the "Simple Memory Leak Detection Code" in VC 6.0 issued by 2004- 09-02. (Deleted) update.
For C code, although there are many methods of memory leakage, the actual code is written, or for confidence or for complexity, it will often use the original Operator New, which inevitable band The possibility of memory leakage, soon, I violated the "Virtual Modification" that can be used to be used for the base class that can be used to be used in polymorphism, and I will forget to write Virtual ^ _ ^), resulting in memory Leak, so I feel that it is necessary to join the memory leak inspection mechanism in the code, but also because this memory leak event prompts me to write this article.
The Memory Leak Check in VC , you can see the following code in the project that supports MFC in the wizard: #ifdef _debug #define new debug_new #undef this_file static char this_file [] = __file__; #ndif passes them, You can discover the memory leak in the code, but if you manually transplant this function into non-MFC project, it is a cumbersome thing, and it has a bug, which will cause this debug_new in multi-threaded and sent this debug_new. System-level error, so I rewrive this function, add the following debug_new.h and debug_new.cpp to the project, and join in the CPP you need to detect. REG_DEBUG_NEW macro can be.
1. Debug_new.h Source Code
/ ************************************************** *********************** / / * Comment: This file is used in conjunction with debug_new.cpp, used to discover the memory leak * // * only applicable in VC compiler (including the Intel C , because it uses the same library) * // * author: Zhou stars http://blog.vckbase.com/bruceteen/ * // * Imprint: None, can be arbitrary Use, modification and release * // *************************************************************** ***************************** /
/ * SAMPLE
#include
INT main (void) {reg_debug_new; //
Char * p = new char [2];
Cout << "--end -" << endl; return 0;}
Press F5 to press F5 in VC to see the following prompts in the output window: DUMPING OBJECTS -> D: /Test.cpp (10): {45} Normal Block AT 0x003410C8, 2 bytes long.data : <> CD CD Object Dump Complete.
If you do not appear as prompted, please refer all.
* /
#ifndef _debug_new_h_ # define _debug_new_h_
#ifdef _Debug
#undef new extern void _RegDebugNew (void); extern void * __cdecl operator new (size_t, const char *, int); extern void __cdecl operator delete (void *, const char *, int); #define new new (__ FILE__, __LINE__ #Define reg_debug_new _regdebugnew (); # else
#define reg_debug_new
#ENDIF / / _DEBUG
#ENDIF / / _DEBUG_NEW_H_
2. Debug_new.cpp Source Code
/ ************************************************** *********************** / / * Comment: This file is used in conjunction with debug_new.h, used to discover the memory leak * // * only applicable in VC compiler (including the Intel C , because it uses the same library) * // * author: Zhou stars http://blog.vckbase.com/bruceteen/ * // * Imprint: None, can be arbitrary Use, modification and release * // *************************************************************** ***************************** /
/ / # include "debug_new.h"
#ifdef _Debug
#include
class _CriSec {CRITICAL_SECTION criSection; public: _CriSec () {InitializeCriticalSection (& criSection);} ~ _CriSec () {DeleteCriticalSection (& criSection);} void Enter () {EnterCriticalSection (& criSection);} void Leave () {LeaveCriticalSection (& criSection); } }_cs;
void _RegDebugNew (void) {_CrtSetDbgFlag (_CRTDBG_REPORT_FLAG | _CRTDBG_LEAK_CHECK_DF);} void * __cdecl operator new (size_t nSize, const char * lpszFileName, int nLine) {// comment 1: MFC provided debug new Although the rate lock, but I When I actually test, I found the multi-threaded concurrency // called, I still throw a system error, so I added a thread mortiest with a thread. // Comment 2: Debug New and Debug delete need no mutual exclusion I don't know, the insurance is, I also add the thread mutex. // Comment 3: According to the C standard, the // function set to set_new_handler should call the set_new_handler after the Operator New, but MSDN said "" header file " The set_new_handler in New is stub, and should make the // _SET_NEW_HANDLER in the header file new.h, which is simply the big picture of the world. // The following is the set_new_handler definition in VC 6.0: // new_handler __cdecl set_new_handler (new_handler new_p) // {// assert (new_p == 0); // canNot USE Stub to register a new handler // _SET_NEW_HANDLER (0); // Return 0; //} // So I also don't work , Only to discard the role of set_new_handler ._cs.enter (); void * p = _malloc_dbg (nsize, _ Normal_block, lpszfilename, nLINE); _CS.LEAVE (); RETURN P;} void __cdecl operator delete (void * p, const char * / * lpszfilename * /, int / * nLINE * /) {_cs.Enter (); _free_dbg (p, _client_block); _cs.leave ();
#ENDIF
3. Sample code
#include
INT main (void) {reg_debug_new;
Char * p = new char [2]; p [0] = 'a'; p [1] = 'b';
Cout << "--end -" << endl; return 0;}
4. Result output