VirtualAllocEx_ questions

Just starting out? Need help? Post your questions and find answers here.
PeterBotes
User
User
Posts: 63
Joined: Tue Nov 15, 2011 2:12 pm

VirtualAllocEx_ questions

Post 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
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: VirtualAllocEx_ questions

Post 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.
PeterBotes
User
User
Posts: 63
Joined: Tue Nov 15, 2011 2:12 pm

Re: VirtualAllocEx_ questions

Post by PeterBotes »

Thanks Thorium
Post Reply