In this question, I suggested an ad-hoc solution to your problem: Immediately detect heap corruption errors in Windows. How?
In general, you can replace your new and delete with this code:
DWORD PageSize = 0; inline void SetPageSize() { if ( !PageSize ) { SYSTEM_INFO sysInfo; GetSystemInfo(&sysInfo); PageSize = sysInfo.dwPageSize; } } void* operator new (size_t nSize) { SetPageSize(); size_t Extra = nSize % PageSize; nSize = nSize + ( PageSize - Extra ); return Ptr = VirtualAlloc( 0, nSize, MEM_COMMIT, PAGE_READWRITE); } void operator delete (void* pPtr) { MEMORY_BASIC_INFORMATION mbi; VirtualQuery(pPtr, &mbi, sizeof(mbi));
to determine invalid memory access. In the following discussion, you will find ideas for detecting leaks and detecting other memory errors.
If you want to call global new and delete , you can use the :: global namespace prefix prefix:
return ::new(nSize);
source share