Hi,
I use VS 2005 SP1
My goal is to be able to track all the allocated memory addresses (by "new" operator) of my application and a large application framework (Qt - qtsoftware.com) linked to my application during the all application lifetime.
Why I need that: I want to log (just on the console or write in a file) the following information about each allocation:
1. Source file where allocation procedure (new) was called
2. Source file line where allocation procedure (new) was called
Another very important thing for me is the place where the memory was deallocated (by "delete" operator)
That will help me in future to find out the place where the memory that is invalid was allocated. It is very helpful.
So what I've already found is DCRT (debug version of VC CRT library). There exists a hook called "_CrtSetAllocHook" which allows to set a custom handler which is called every time the memory is allocated, deallocated or reallocated. Unfortunatelly, when it is called as "allocation" hook, the pointer passed is 0x00 because the memory has not been alocated yet and there is no way (I believe) to get a pointer to allocated memory in such kind of hooks.
What I've already discovered: If I call the degugging version of "new" operator, it calls "_nh_malloc_dbg" method and passes there the file name and source line of the call where the allocation function was called. Therefore installing the following hook:
int YourAllocHook(int allocType, void *userData, size_t size, int blockType, long requestNumber, const unsigned char *filename, int lineNumber)
{
static bool entered = false;
if (entered)
return true;
entered = true;
if (allocType == _HOOK_FREE) {
_CrtMemBlockHeader * pHead = pHdr(userData);
printf("memory pointer = %p\n", userData);
printf("filename = %s\n", pHead->szFileName);
printf("lineNumber = %d\n\n", pHead->nLine);
}
entered = false;
return true;
}
int main()
{ ....
_CrtSetAllocHook(YourAllocHook);
...
}
will help to retrieve the necessary information about where the memory was allocated in the "deallocation" hook, which nearly what I need: I can get at least allocation information.
Take a look at hack I made:
_CrtMemBlockHeader * pHead = pHdr(userData);
It is possible to do only defining "_CRTBLD" and including "dbgint.h" (alsough is possible to do without that by manual cast).
Another trick is a "static" flag variable which allows to avoid recursive calls because "printf" also allocated memory inside.
So what is my current problem: I want to replace the standard "new(size_t _Size)" call with debugging "new(size_t cb, int nBlockUse, const char * szFileName, int nLine)" which allows to pass the source file and line number to print them in deallocation procedure, because the standard "new" doesn't do that.
I need to replace it in both my application code as well as Qt code.
First I tried to make the follwing:
#define new new( _CLIENT_BLOCK, __FILE__, __LINE__)
But compiler finished with error claiming that the operator "new" is redefined.
Another way may be to find/replace the "new" operator throughout the whole source code (as well as all source code of Qt framework that I use) and rebuild everything, but I really don't like this solution. Maybe some newer VS (my is VS 2005 SP1) have some functionality to do what I want ?
Is there any other way to replace the "new" operator with the dubugging version of it ?
Looking forwared to hear from anybody as I desperately need this functionality.
Thanks