In VC ++ 6.0, memory leakage detection Source: deleted updates.
For C ++ Code, although there are many ways to avoid Memory leakage, the actual code may be written out of confidence or complexity, the original operator new is often used, which may inevitably lead to memory leakage, not long ago, because I violated the rule that "the destructor of the base class that can be used to be inherited by polymorphism should have virtual modifier" (I accidentally forgot to write virtual ^_^ ), this results in Memory leakage. Therefore, I think it is necessary to add the memory leakage check mechanism to the Code for security reasons. This memory leakage event prompted me to write this article.
In VC ++, there is a memory leak check mechanism. You can see the following code in the project that supports MFC generated by the Wizard:
# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif
With these features, you can easily find memory leaks in the code, but it is very cumbersome to manually port this function to a non-MFC project, in addition, it has a bug that system-level errors may occur when multiple threads concurrently call debug_new. Therefore, I have rewritten this function here and added the following debug_new.h and debug_new.cpp to the project, add the reg_debug_new macro at the beginning of the CPP # include "debug_new.h" and main to be checked.
1. debug_new.h source code /************************************* ***********************************/
/* Comment: This file is used with debug_new.cpp to detect memory leaks during debugging */
/* Applicable only to VC ++ compilers (including Intel C ++ because it uses the same library )*/
/* Author: Star week http://blog.vckbase.com/bruceteen */
/* Copyright statement: None, available for any use, modification, and release */
/*************************************** *********************************/
/* Sample
# Include <iostream>
# Include "debug_new.h" // +
Using namespace STD;
Int main (void)
{
Reg_debug_new; // +
Char * P = new char [2];
Cout <"-- end --" <Endl;
Return 0;
}
Press F5 in VC ++ IDE for debugging. The following prompt is displayed on the debug page of the output window:
Dumping objects->
D:/test. cpp (10): {45} normal block at 0x003410c8, 2 bytes long.
Data: <> Cd
Object dump complete.
If the above prompt is not displayed, please rebuild all once.
*/
# 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 (_ 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 with debug_new.h to detect memory leaks during debugging */
/* Applicable only to VC ++ compilers (including Intel C ++ because it uses the same library )*/
/* Author: Star week http://blog.vckbase.com/bruceteen */
/* Copyright statement: None, available for any use, modification, and release */
/*************************************** *********************************/
// # Include "debug_new.h"
# Ifdef _ debug
# Include <windows. h>
# Include <crtdbg. h>
Class _ criec
{
Critical_section criection;
Public:
_ Criec () {initializecriticalsection (& criection );}
~ _ Criec () {deletecriticalsection (& criection );}
Void enter () {entercriticalsection (& criection );}
Void leave () {leavecriticalsection (& criection );}
} _ 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: although the debug new provided by MFC is locked, I found the multi-thread concurrency during actual testing.
// A system error is thrown during the call, so I add a thread mutex here.
// Comment 2: mutual exclusion between debug new and debug Delete is not required. I am not sure. For the sake of insurance, I am the same
// Adds the thread mutex.
// Comment 3: According to the C ++ standard, the set_new_handler setting should be called after operator new fails.
// Function, but msdn says, "The set_new_handler in the header file new is stub, instead
// Use the _ set_new_handler in the header file new. h.
// 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 have no choice but 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. Example code # include <iostream>
# Include "debug_new.h"
Using namespace STD;
Int main (void)
{
Reg_debug_new;
Char * P = new char [2];
P [0] = 'a ';
P [1] = 'B ';
Cout <"-- end --" <Endl;
Return 0;
}
4. The result output is debugged by pressing F5 in VC ++ ide. The following prompt is displayed on the debug page of the output window:
......
Dumping objects->
D:/test. cpp (10): {45} normal block at 0x003410c8, 2 bytes long.
Data: <AB> 41 42
Object dump complete.
......