I am debugging a multi-threaded C++ application under Visual Studio 2017. The type of problem can be reproduced with the following code sample
int* i = new int();
*i = 4;
int* j = i;
delete i;
//perhaps some code or time passes
*j = 5;//write to freed momory = possible heap corruption!!
I've used the built in heap checker to find the type of problem with flags:
_CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_DELAY_FREE_MEM_DF )
Then used _ASSERTE(_CrtCheckMemory());
to try to narrow it down - but only to conclude it was in another thread. Looking at other threads by the time the corruption was detected seems to just be doing 'normal' stuff and not be in the app's code at the time.
Reports look like this:
HEAP CORRUPTION DETECTED: on top of Free block at 0x00000214938B88D0.
CRT detected that the application wrote to a heap buffer that was freed.
DAMAGED located at 0x00000214938B88D0 is 120 bytes long.
Debug Assertion Failed!
Everytime 120 bytes - but the address varies. (the way it's detected is that the 0xdddddddd pattern has been overwritten when the heap is checked) Either finding the allocation or finding the offending write would be helpfull. I've tried to use 'gflags.exe', but I was unable to find this type of corruption (as I understand its mainly designed to find buffer overruns) and I do not have previous experience with this tool. If I could find the "unique allocation number" from the address, that might also be helpfull.