I use "data breakpoints" for this. In your case, when the program crashes, it may first file a complaint as follows:
Heap block at 00397848 changed to 0039789C with requested size 4c
Then run your program again and set the data breakpoint at 0039789C
. When the code writes to this address, execution stops. It often happens that I found an error right at that moment.
If your program allocates and frees memory again, and it turns out that it is at this exact address that you simply disable deallocation:
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_DELAY_FREE_MEM_DF);
source share