Detecting memory leaks in VCs

Source: Internet
Author: User

Disclaimer: Checkleaks.h and checkleaks.cpp The code in these two files is copied online, but the original site is not found now

Therefore, this article is not completely original, hehe.

Just think of it as an archive.

First on the code:

[CPP]View Plaincopyprint?
  1. Checkleaks.h
  2. #ifndef Set_debug_new_h
  3. #define Set_debug_new_h
  4. #ifdef _DEBUG
  5. #define Debug_clientblock New (_client_block, __file__, __line__)
  6. #else
  7. #define Debug_clientblock
  8. #endif
  9. #define _crtdbg_map_alloc
  10. #include <crtdbg.h>
  11. #ifdef _DEBUG
  12. #define NEW Debug_clientblock
  13. #endif
  14. #pragma once
  15. #if defined (WIN32)
  16. void Setfilterdebughook (void);
  17. #endif
  18. #endif
  19. Checkleaks.cpp
  20. #if defined (WIN32)
  21. #include <string.h>
  22. #include "crtdbg.h"
  23. #define FALSE 0
  24. #define TRUE 1
  25. _crt_report_hook Prevhook;
  26. int Reportinghook (int reporttype, char* usermessage, int* retVal)
  27. {
  28. //This function was called several times for each memory leak.
  29. //Each time a part of the error message is supplied.
  30. //This holds number of subsequent detail messages after
  31. //A leak was reported
  32. const int numfollowupdebugmsgparts = 2;
  33. static bool ignoremessage = false;
  34. static int debugmsgpartscount = 0;
  35. //Check if the memory leak reporting starts
  36. if (strncmp (usermessage,"detected memory leaks!/n", ten) = = 0)
  37. || Ignoremessage)
  38. {
  39. //Check if the memory leak reporting ends
  40. if (strncmp (usermessage,"Object dump complete./n", ten) = = 0)
  41. {
  42. _CrtSetReportHook (Prevhook);
  43. Ignoremessage = false;
  44. } Else
  45. Ignoremessage = true;
  46. //Something from our own code?
  47. if (strstr (Usermessage, ". cpp") = = NULL)
  48. {
  49. if (debugmsgpartscount++ < numfollowupdebugmsgparts)
  50. //Give it back to _CrtDbgReport () to being printed to the console
  51. return FALSE;
  52. Else
  53. return TRUE; //Ignore it
  54. } Else
  55. {
  56. Debugmsgpartscount = 0;
  57. //Give it back to _CrtDbgReport () to being printed to the console
  58. return FALSE;
  59. }
  60. } Else
  61. //Give it back to _CrtDbgReport () to being printed to the console
  62. return FALSE;
  63. };
  64. void Setfilterdebughook (void)
  65. {
  66. //change the "report function" to "Report memory leaks " from program code
  67. Prevhook = _CrtSetReportHook (Reportinghook);
  68. }
  69. #endif
  70. Main.cpp
  71. #include <windows.h>
  72. #include <stdio.h>
  73. #include "Checkleaks.h"
  74. int main ()
  75. {
  76. //Be sure to add this sentence and have the report output in debug mode
  77. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _crtdbg_leak_check_df);
  78. int *arr;
  79. arr = new int;
  80. return 0;
  81. }


Program output:

[CPP]View Plaincopyprint?
      1. Detected memory leaks!
      2. Dumping objects
      3. E:\vs2008\Try\ctemp\ctemp\main.cpp (9): {$} client block at 0x00503de8, subtype 0, 4 bytes long.
      4. Data: < > CD CD CD
      5. Object dump complete.
      6. The program ' [3348] ctemp.exe:Native ' have exited with code 0 (0x0).

http://blog.csdn.net/small_qch/article/details/6856445

Detecting memory leaks in VCs

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.