I am on windows10 and virtualAlloc fails with error code 487 (accessing invalid memory address) whenever I pass a baseAddress. This is done for looped live code editing in the hmh streams. I tried to find any solutions on google and the closest thing I got was some advise about never allocating with a baseAddress.
An interesting post at the end of this thread says that someone has two identical(ish?) devices running windows 7 and one always succeeds in allocating at the address and the other always fails. And the poster is asking if maybe there is some os level setting to enable this.
Has anyone else faced the always failing problem in windows 10 and if so how did you solve it?
Could you make a small and simple reproduction case that we could try on our end to be sure it's not an issue with your code ?
Yeah so I wrote the following:
#include <windows.h> #include <stdio.h> #include <stdint.h> #define Kilobytes(count) ((count) * 1024) #define Megabytes(count) (Kilobytes(count) * 1024) #define Gigabytes(count) (Megabytes(count) * 1024) #define Terabytes(count) (Gigabytes(count) * 1024) int WINAPI wWinMain( HINSTANCE Instance, HINSTANCE PrevInstance, PWSTR CommandLine, int ShowCode) { LPVOID BaseAddress = (LPVOID)Terabytes((uint64_t)2); /********In here if I replace BaseAddress with 0 it works****/ void *Mem = VirtualAlloc(BaseAddress, 64 * 1024, MEM_COMMIT, PAGE_READWRITE); /*******************^^*****************/ DWORD Error = GetLastError(); char Buffer[256]; printf("Error code: %d\n", Error); if (Mem) { free(Mem); printf("Memory was allocated and freed\n"); } else { printf("Memory was not allocated\n"); } return 0; }
I am attaching 2 files showing the success and error runs in visual studio. Look at the watch window in the bottom left to see the values of the error and pointer to the memory allocated. You may have to open the images in a new tab to actually see the small text.
It looks like the flAllocationType parameter is wrong for VirtualAlloc.
"Attempting to commit a specific address range by specifying MEM_COMMIT without MEM_RESERVE and a non-NULL lpAddress fails unless the entire range has already been reserved. The resulting error code is ERROR_INVALID_ADDRESS."
Also, memory allocated with VirtualAlloc should be freed with VirtualFree instead of free.
@William thank you. This worked. I should have read the docs more thoroughly.