Statement: the code in the checkleaks. h and checkleaks. cpp files is copied online, but the website cannot be found now.
Therefore, this article is not completely original.
As an archive.
First run the Code:
// Checkleaks. h # ifndef set_debug_new_h # define set_debug_new_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 # pragma once # If defined (win32) void setfilterdebughook (void); # endif // checkleaks. CPP # If defined (win32) # include <s Tring. h> # include "crtdbg. H "# define false 0 # define true 1 _ crt_report_hook prevhook; int reportinghook (INT reporttype, char * usermessage, int * retval) {// This function is called several times for each memory leak. // each time a part of the error message is supplied. // This holds number of subsequent detail messages after // a leak was reported const int numfollowupdebugmsgparts = 2; static B OOl ignoremessage = false; static int debugmsgpartscount = 0; // check if the memory leak reporting starts if (strncmp (usermessage, "detected memory leaks! /N ", 10) = 0) | ignoremessage) {// check if the memory leak reporting ends if (strncmp (usermessage," Object dump complete. /n ", 10) = 0) {_ crtsetreporthook (prevhook); ignoremessage = false;} else ignoremessage = true; // something from our own code? If (strstr (usermessage ,". CPP ") = NULL) {If (debugmsgpartscount ++ <numfollowupdebugmsgparts) // give it back to _ crtdbgreport () to be printed to the console return false; else return true; // ignore it} else {debugmsgpartscount = 0; // give it back to _ crtdbgreport () to be printed to the console return false ;}} else // give it back to _ crtdbgreport () to be printed to the console return false;}; void setfilterdebughook (void) {// change the report function to only report memory leaks from program code prevhook = _ crtsetreporthook (reportinghook);} # endif // main. CPP # include <windows. h> # include <stdio. h> # include "checkleaks. H "int main () {// be sure to add this sentence. In debug mode, the report output _ crtsetdbgflag (_ crtdbg_alloc_mem_df | _ crtdbg_leak_check_df); int * arr; arr = new int; return 0 ;}
Program output:
Detected memory leaks!Dumping objects ->e:\vs2008\try\ctemp\ctemp\main.cpp(9) : {62} client block at 0x00503DE8, subtype 0, 4 bytes long. Data: < > CD CD CD CD Object dump complete.The program '[3348] ctemp.exe: Native' has exited with code 0 (0x0).