// --------------------------------------------------------------- // File: Check_Memory_Demo.cpp // Purpose: Demonstrate how to: // - Access Debug heap's state // - Have the DCRT check memory functionality // to track memory leakks & overwrites. // Author: Divyanand Kutagulla, dkutagulla@hotmail.com // Source: Run-time library reference VS.NET 2003 help system // Course: Software Testing Seminar, Spring 2004 // --------------------------------------------------------------- // 1 --------------------------------------------------------- // Define _CRTDBG_MAP_ALLOC symbol before everything. // This Symbol MUST be the first line in the file so that // first during preprocessing even before any header files // (specified in the include directives) are processed. // This define sets up the mapping of the // memory allocation and deallocation routines (malloc/new/ // free/delete) to the special debug versions that record // the file name and line number within the file where the // memory request was made #define _CRTDBG_MAP_ALLOC //--------------------------------------------------------- // 2 ------------------------------------------------------ // all the include files that we want to include go here // after the _CRTDBG_MAP_ALLOC has been #defined above #include "..\Common\myclass.h" #include "..\Common\dcrt_setup.h" #include //------------------------------------------------------ // 3 ----------------------------------------------------- // finally include the Debug C runtime header #include //----------------------------------------------------- using namespace std; void line() { cout << "--------------------------------------------------" << endl; } //display the contents of the memory state structure... //same as calling _CrtMemDumpStatistics(&ckpt1) void print_mem_state (_CrtMemState& ckpt1) { line(); cout << "Displaying the Debug heap state..." << endl; line(); size_t num_free_blocks =0; num_free_blocks = ckpt1.lCounts[_FREE_BLOCK]; cout << "Number of free blocks alllocated so far: " << num_free_blocks << endl; size_t num_normal_blocks = 0; num_normal_blocks = ckpt1.lCounts[_NORMAL_BLOCK]; cout << "Number of normal blocks alllocated so far: " << num_normal_blocks << endl; size_t num_crt_blocks = 0; num_crt_blocks = ckpt1.lCounts[_CRT_BLOCK]; cout << "Number of normal blocks alllocated so far: " << num_crt_blocks << endl; cout << "NOTE: You really don't wanna mess around with these guys!!!!" << endl; size_t num_ignore_blocks = 0; num_ignore_blocks = ckpt1.lCounts[_IGNORE_BLOCK]; cout << "Number of ignore blocks alllocated so far: " << num_ignore_blocks << endl; size_t num_client_blocks = 0; num_client_blocks = ckpt1.lCounts[_CLIENT_BLOCK]; cout << "Number of client blocks alllocated so far: " << num_client_blocks << endl; line(); } void memory_state_demo() { //store the state of memory ( a memory checkpoint ) _CrtMemState ckpt0; _CrtMemCheckpoint(&ckpt0); myclass2* p1 = new myclass2("hello"); //store the state of memory ( a memory checkpoint ) _CrtMemState ckpt1; _CrtMemCheckpoint(&ckpt1); //now lets print the memory state... print_mem_state(ckpt1); myclass2* p2 = new myclass2("hello"); //store the state of memory ( a memory checkpoint ) _CrtMemState ckpt2; _CrtMemCheckpoint(&ckpt2); //now lets print the memory state... print_mem_state(ckpt2); delete p1; //demonstrate how to use //_CrtMemDumpStatistics in conjunction with _CrtMemDifference _CrtMemState ckpt3; if(_CrtMemDifference(&ckpt3, &ckpt1, &ckpt2)) { cout << "deleted p1 and still p2 is allocated..." << endl; cout << "the meory statistics dumped to the Debug window shows this" <