//////////////////////////////////////////////////////////////// //File: dcrt_setup.h //Purpose: DCRT setup using the appropriate flags //Author: Divyanand Kutagulla, dkutagulla@hotmail.com //source: Run-time library reference VS.NET 2003 help system //Course: Software Testing Seminar, Spring 2004 //////////////////////////////////////////////////////////////// #ifndef DCRT_SETUP_H #define DCRT_SETUP_H #include //must include this for getting Debug C runtime #include //exposes wrapper functions for setting and unsetting the DCRT flags class DCRT { public: //Wrappers for the original DCRT flags. //The flags have been given more meaningful names. enum DCRT_FLAG { //Turn on the DCRT memory checking functionality. when the DCRT class is constructed, //this flag is turned on by default. START_DCRT_MEMORY_TRACKING = _CRTDBG_ALLOC_MEM_DF, //On each memory allocation/deallocation check the integrity of the debug //heap i.e. check for any overwrites/underwrites. This will catch the most //recent integrity violation (overwirte/underwrite). TRACK_EVERY_MEM_REQ = _CRTDBG_CHECK_ALWAYS_DF, //Display Memory leaks at application exit SHOW_MEM_LEAKS_AT_APP_EXIT = _CRTDBG_LEAK_CHECK_DF, //Track even CRT's memory allocations and include them in the memory leak reports //Typically the DCRT allocates some memory for its internal use. If this //flag is set, then we will get some false positive memory leak report at //app exit since these memory are not freed until later. //Typically we don't set this flag TRACK_CRT_MEM_REQ = _CRTDBG_CHECK_CRT_DF, //When free/delete is called, do not free any allocated memory locations //instead, keep the, in the allocation list and mark them as being freed //by putting a special hexadecimal value (OxDD) in those locations DELAY_MEM_DEALOCATION = _CRTDBG_DELAY_FREE_MEM_DF }; //ctor DCRT(); //set a DCRT flag to any of values shown above void set_flag(DCRT_FLAG flag); //clear a DCRT flag to any of values shown above void clear_flag (DCRT_FLAG flag); //retrieve the current state of all DCRT flags int getAllFlags(); //dtor ~DCRT(); private: int original_flags; }; DCRT::DCRT() { //save the original flags of the DCRT library original_flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); //start the memory tracking int temp = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); temp |= _CRTDBG_ALLOC_MEM_DF; _CrtSetDbgFlag( temp ); } //Set the specified DCRT flag void DCRT ::set_flag(DCRT_FLAG flag) { // Query the DCRT to to get the current flag // Get the current state of the flag // and store it in a temporary variable int temp = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); //set a flag by logical ORing desired flag value into the DCRT's //internal flag variable /* here flag can be any of : _CRTDBG_ALLOC_MEM_DF, _CRTDBG_CHECK_ALWAYS_DF, _CRTDBG_LEAK_CHECK_DF, _CRTDBG_CHECK_CRT_DF, _CRTDBG_DELAY_FREE_MEM_DF */ temp |= flag; _CrtSetDbgFlag( temp ); } //Clear the specified DCRT flag void DCRT::clear_flag(DCRT_FLAG flag) { // Query the DCRT to to get the current flag // Get the current state of the flag // and store it in a temporary variable int temp = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); //clear a flag by logical ANDing the negation of the desired flag value into the DCRT //internal flag variable /* here flag can be any of : _CRTDBG_ALLOC_MEM_DF, _CRTDBG_CHECK_ALWAYS_DF, _CRTDBG_LEAK_CHECK_DF, _CRTDBG_CHECK_CRT_DF, _CRTDBG_DELAY_FREE_MEM_DF */ temp &= ~flag; _CrtSetDbgFlag( temp); } int DCRT::getAllFlags() { return _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); } //Restore the original DCRT library flags //after dumping any memory leaks DCRT::~DCRT() { //We know that we have dump memory leaks when we find the //TRACK_MEM_LEAKS_AT_APP_EXIT flag set to 1 if(this->getAllFlags() & DCRT::SHOW_MEM_LEAKS_AT_APP_EXIT) { _RPT0( _CRT_WARN, "Dunping details of any memory leaks found during app run...\n"); _CrtDumpMemoryLeaks(); } //memory tacking will automatically revert to its original value //when we restore the original DCRT flags _RPT0( _CRT_WARN, "Restoring the original DCRT state...\n" ); _CrtSetDbgFlag( original_flags ); } #endif