Page 1 of 1

VirtualAllocEx_ questions

Posted: Mon Feb 20, 2012 1:55 pm
by PeterBotes
Hi All,

I have a problem with memory and I am trying to track it down, I suspect VirtualAllocEx_ but wanted to get a little feedback from you guys.

I have seen VirtualAllocEx_ used in many places in this forum and others but two things perplex me

1) Why do 99% of the examples not RESERVE the memory but just COMMIT it, from what I have read you should do the following

Code: Select all

VirtualAllocEx_(hprocess, 0, 2000, #MEM_RESERVE | #MEM_COMMIT , #PAGE_READWRITE)
2) Why, in most examples do people NOT call FlushInstructionCache when allocating memory for code?
[Edit] Found out that this is, it is not needed on x86.

3) Finally for now, I did ask in a older post if "I should free" allocated memory and got the answer that the process will deal with deallocation, I have seen some examples were people call VirtualFreeEx, can anyone confirm if I need to clean up or not?


Thanks

Pete

Re: VirtualAllocEx_ questions

Posted: Wed Feb 22, 2012 12:22 am
by Thorium
1.) Dont know why its not done that way in some codes but using reserve and commit together is correct. I do it that way and MSDN says it should be done this way.

2.) Not needed.

3.) That depents. Windows will free all the memory if the process terminates. However you are using VirtualAllocEx, which means you are allocating memory in another process. You should free it if you dont need it any longer.

Re: VirtualAllocEx_ questions

Posted: Thu Feb 23, 2012 10:53 pm
by PeterBotes
Thanks Thorium