Detect memory leakage in VC

Source: Internet
Author: User

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).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.