/ 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.
Expand |
Embed | Plain Text
#define CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> //#include <vld.h> #ifdef _DEBUG #define new new(_NORMAL_BLOCK, __FILE__, __LINE__) #endif #define STATIC_LEAK #ifdef STATIC_LEAK #include <iostream> // "Foo" simply logs its creation and destruction. class Foo { std::string name; public: Foo( std::string const & name ) : name(name) { std::cout << "Foo instance \"" << name << "\" created.\n"; } ~Foo() { std::cout << "Foo instance \"" << name << "\" destroyed.\n"; } }; // define a global "Foo" instance - accessible from other modules Foo global("global"); // create a local Foo instance - accessible only within this source file (C-style) static Foo staticFoo("local (static)"); namespace { // create a local Foo instance - accessible only within this source file (C++ style) Foo localFoo("local (anonymous namespace)"); // atexit handler, called on program shutdown void report() { std::cout << "atexit handler called\n"; } } // Same as "Foo" but contains a static member variable class Bar { // static Foo instance - only accessible by "Bar" instances static Foo foo; std::string name; public: Bar( std::string const & name ) : name( name ) { std::cout << "Bar instance \"" << name << "\" created.\n"; } ~Bar() { std::cout << "Bar instance \"" << name << "\" destroyed.\n"; } }; // static member variable contained by "Bar" Foo Bar::foo("Bar::foo"); // global "Bar" instance, also accessible by other modules Bar b("global"); #endif //STATIC_LEAK int main(int argc, char* argv[]) { _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); //_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF); #ifdef STATIC_LEAK atexit(report); // define some variables within the function's scope Foo a("in main a"), b("in main b"), c("in main c"); Bar d("in main"); #endif // STATIC_LEAK //_CrtSetBreakAlloc(125); _CrtDumpMemoryLeaks(); return 0; }
Comments
Subscribe to comments
You need to login to post a comment.

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.