#include <Windows.h>
#include <iostream>
using namespace std;
int main(void)
{
unsigned char* pFoo = new unsigned char[1000];
pFoo = (unsigned char*)VirtualAlloc(NULL, 1000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
VirtualFree(pFoo, 0, MEM_RELEASE);
delete[] pFoo;
cin.ignore();
cin.get();
return 0;
}
This crashes for me at
delete[] pFoo;
I know this is crashing because of VirtualAlloc but I'm not sure how to fix this...
You're using the same variable. So your first allocation gets leaked.
After you free it with VirtualFree
, the pointer is invalid. So delete
on it is undefined.
Furthermore:
You can't mix VirtualAlloc
and delete
for the same reason you can't mix malloc
with delete
.