Maybe someone on here can shed some light on why this is the case or what other calls to use for walking the heap for debugging purposes in an application. If it turns out that there is no way of getting Page Heap and / or the Application Verifier to work with an application that uses HeapWalk() and there is no replacement - at least I can stop looking for one :)
So a confirmation from people with more experience on the matter that these will never play together would be appreciated.
Nothing in the documentation on HeapWalk() I could find mentions problems with Page Heap / Application Verifier or suggests any replacement for this use case.
Considering the small sample code below:
If the code is executed "as is" it will work as expected. If I turn on either Page Heap or Application Verifier (or both) for it the call to HeapWalk() fails. The error code returned by GetLastError() in this case is always 0x00000001 which regarding to the Microsoft documentation is "ERROR_INVALID_FUNCTION".
Underlying motivation for the question:
Track down heap corruption and potential memory leaks in a legacy application with custom heap management. Running with Page Heap and / or Application Verifier was intended to help with that.
Thanks for reading this and any comments that could shed some light unto the issue are welcome.
#include <Windows.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
size_t DummyHeapWalk(void* heapHandle)
{
size_t commitSize = 0;
PROCESS_HEAP_ENTRY processHeapEntry;
processHeapEntry.lpData = 0;
HeapLock(heapHandle);
if (HeapWalk(heapHandle, &processHeapEntry))
{
commitSize = processHeapEntry.Region.dwCommittedSize;
}
else
{
int lastError = GetLastError();
std::ostringstream errorMsg;
errorMsg << "DummyHeapWalk failed on heapHandle [0x" << std::setfill('0') << std::setw(16) << std::hex << (size_t)(heapHandle) << "] last error: " << lastError << std::endl;
OutputDebugString(errorMsg.str().c_str());
DebugBreak();
}
HeapUnlock(heapHandle);
return commitSize;
}
int main(void)
{
HANDLE myProcess = GetProcessHeap();
std::cout << "Process Heap Commit size: " << DummyHeapWalk(myProcess) << std::endl;
return 0;
}