// --------------------------------------------------------------- // File: Check_Memory_Demo.cpp // Purpose: How to use the Memory allocation DCRT hooks to hook custom // processing that happens during every memory allocation/deallocations // Author: Divyanand Kutagulla, dkutagulla@hotmail.com // Source: Run-time library reference VS.NET 2003 help system // Course: Software Testing Seminar, Spring 2004 // --------------------------------------------------------------- #include #include //example class #include "..\Common\myclass.h" //wrapper class for the DCRT flags #include "..\Common\dcrt_setup.h" //DCRT runtime files #include //Contains the macro to fix the problem with the //memory leak reports for memory allocated using the //new operator #include "..\Common\fix_new.h" using namespace std; //the allocation hook function int myclassAllocHook( int nAllocType,\ void *pvData,\ size_t nSize,\ int nBlockUse,\ long lRequest,\ const unsigned char * szFileName,\ int nLine ) { switch(nAllocType) { case _HOOK_ALLOC: _RPT1(_CRT_WARN, "Allocation of memory for myclass object of size %d bytes. \n", nSize); _RPT2(_CRT_WARN, "Allocation occured in file: %s at line %d .\n", szFileName, nLine); _RPT0(_CRT_WARN, "-----------------------------------------------------------------\n"); return TRUE; //allow the allocation to proceed case _HOOK_REALLOC: return TRUE;//allow the allocation to proceed case _HOOK_FREE: //Unlike the debug version of the new operator, the debug version of the delete operator does not //report any extra information (like the source file where the memory deletion occured). //It only performs the deletion. //Dunno how to get the source and line info into this hook for deletion _RPT1(_CRT_WARN, "DeAllocation of memory for myclass object of size %d bytes. \n", sizeof(*((myclass*)pvData))); _RPT0(_CRT_WARN, "-----------------------------------------------------------------\n"); return TRUE;//allow the allocation to proceed default: return FALSE; //some wrong with the DCRT dont allow the allocation to proceed!!! } } myclass** allocateMem(int size) { myclass** vec= new myclass*[size]; char str[10]; for(int i=0; i