Posted By

maxint on 09/01/11


Tagged

vc


Versions (?)

memory leak detection of static class variables


 / Published in: C++
 

URL: http://www.gamedev.net/topic/400705-memory-leak-detection-of-static-class-variables/

VC crtdbg detects the memory leaks of static variables by mistake

output:

Dumping objects ->
{144} normal block at 0x00395F80, 8 bytes long.
 Data: <        > BC FE 12 00 00 00 00 00 
{142} normal block at 0x00395F38, 8 bytes long.
 Data: <        > E4 FE 12 00 00 00 00 00 
{140} normal block at 0x00395EF0, 8 bytes long.
 Data: <        > 0C FF 12 00 00 00 00 00 
{138} normal block at 0x00395EA8, 8 bytes long.
 Data: <4       > 34 FF 12 00 00 00 00 00 
{136} normal block at 0x00395E60, 8 bytes long.
 Data: <  A     > A0 F1 41 00 00 00 00 00 
{134} normal block at 0x00395D58, 8 bytes long.
 Data: <  A     > 00 F2 41 00 00 00 00 00 
{132} normal block at 0x00395E00, 32 bytes long.
 Data: <local> 6C 6F 63 61 6C 20 28 61 6E 6F 6E 79 6D 6F 75 73 
{131} normal block at 0x00395DB8, 8 bytes long.
 Data: <  A     > E0 F1 41 00 00 00 00 00 
{128} normal block at 0x00395D10, 8 bytes long.
 Data: <  A     > 20 F2 41 00 00 00 00 00 
{126} normal block at 0x00395CC8, 8 bytes long.
 Data: <  A     > C0 F1 41 00 00 00 00 00 
Object dump complete.
  1. #define CRTDBG_MAP_ALLOC
  2. #include <stdlib.h>
  3. #include <crtdbg.h>
  4. //#include <vld.h>
  5.  
  6. #ifdef _DEBUG
  7. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  8. #endif
  9.  
  10. #define STATIC_LEAK
  11.  
  12. #ifdef STATIC_LEAK
  13. #include <iostream>
  14.  
  15. // "Foo" simply logs its creation and destruction.
  16. class Foo
  17. {
  18. std::string name;
  19. public:
  20. Foo( std::string const & name ) : name(name) {
  21. std::cout << "Foo instance \"" << name << "\" created.\n";
  22. }
  23. ~Foo() {
  24. std::cout << "Foo instance \"" << name << "\" destroyed.\n";
  25. }
  26. };
  27.  
  28. // define a global "Foo" instance - accessible from other modules
  29. Foo global("global");
  30.  
  31. // create a local Foo instance - accessible only within this source file (C-style)
  32. static Foo staticFoo("local (static)");
  33.  
  34. namespace
  35. {
  36. // create a local Foo instance - accessible only within this source file (C++ style)
  37. Foo localFoo("local (anonymous namespace)");
  38. // atexit handler, called on program shutdown
  39. void report() {
  40. std::cout << "atexit handler called\n";
  41. }
  42. }
  43.  
  44. // Same as "Foo" but contains a static member variable
  45. class Bar
  46. {
  47. // static Foo instance - only accessible by "Bar" instances
  48. static Foo foo;
  49. std::string name;
  50. public:
  51. Bar( std::string const & name ) : name( name ) {
  52. std::cout << "Bar instance \"" << name << "\" created.\n";
  53. }
  54. ~Bar() {
  55. std::cout << "Bar instance \"" << name << "\" destroyed.\n";
  56. }
  57. };
  58.  
  59. // static member variable contained by "Bar"
  60. Foo Bar::foo("Bar::foo");
  61. // global "Bar" instance, also accessible by other modules
  62. Bar b("global");
  63.  
  64. #endif //STATIC_LEAK
  65.  
  66. int main(int argc, char* argv[])
  67. {
  68. _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
  69. //_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
  70.  
  71. #ifdef STATIC_LEAK
  72. atexit(report);
  73. // define some variables within the function's scope
  74. Foo a("in main a"), b("in main b"), c("in main c");
  75. Bar d("in main");
  76. #endif // STATIC_LEAK
  77.  
  78. //_CrtSetBreakAlloc(125);
  79.  
  80. _CrtDumpMemoryLeaks();
  81.  
  82. return 0;
  83. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: maxint on September 1, 2011

output:
Dumping objects -> {144} normal block at 0x00395F80, 8 bytes long. Data: < > BC FE 12 00 00 00 00 00 {142} normal block at 0x00395F38, 8 bytes long. Data: < > E4 FE 12 00 00 00 00 00 {140} normal block at 0x00395EF0, 8 bytes long. Data: < > 0C FF 12 00 00 00 00 00 {138} normal block at 0x00395EA8, 8 bytes long. Data: 34 FF 12 00 00 00 00 00 {136} normal block at 0x00395E60, 8 bytes long. Data: < A > A0 F1 41 00 00 00 00 00 {134} normal block at 0x00395D58, 8 bytes long. Data: < A > 00 F2 41 00 00 00 00 00 {132} normal block at 0x00395E00, 32 bytes long. Data: 6C 6F 63 61 6C 20 28 61 6E 6F 6E 79 6D 6F 75 73 {131} normal block at 0x00395DB8, 8 bytes long. Data: < A > E0 F1 41 00 00 00 00 00 {128} normal block at 0x00395D10, 8 bytes long. Data: < A > 20 F2 41 00 00 00 00 00 {126} normal block at 0x00395CC8, 8 bytes long. Data: < A > C0 F1 41 00 00 00 00 00 Object dump complete.

You need to login to post a comment.