Day 014 - Memory allocated by VirtualAlloc doesn't get freed

Hi all! FIrst of all, sorry for my poor english. I'm not a native speaker..
I just finished my day 14, and found that there is no VirtualFree call correspondent to each VirtualAlloc calls.
Is that because the memory allocated by VIrtualAlloc doesn't have to be freed? Or Casey doesn't care about memory leak because this is not the final platform layer?
There is no point in freeing the memory; it will be released by the operating system when the Handmade Hero process exits.
Thank you for responding! I just thought VirtualFree should be called for each VIrtualAlloc calls for some reason.
It depends on how you are allocating...

If you are calling a allocator in your application repeatedly, or an unknown amount of times (like its in a loop for example) then you are going to run out of memory eventually if you never free it. If you are done using the memory, then you should free it.

Having a coding style rule that simply says "for every call to VirtualAlloc you need to have a corresponding call to VirtualFree" might be a good idea as a starting point, but really isn't a comprehensive guide to how to plan your memory usage.

For example it might be very difficult or awkward to literally match up frees and allocs in your code if there aren't symmetrical alloc and free paths. (Like multiple ways a state gets allocated, but one way it gets free'd, or vice-versa.)

Instead of having a hard-and-fast rule on how to write code, a simpler approach tends to be just to think about when you are going to need more memory for and how long you are going to need it... but honestly you are probably just better off using memory from the stack most of the time...

Also, if the only path to freeing your memory is when your application exists, then calling a free on it, is just "wasting your user's time"
The best way to think about it is ExitProcess does an implicit VirtualFree for every unfreed VirtualAlloc outstanding. So for any VirtualAlloc calls you don't intend to free until the end of the program, there is no need for a VirtualFree. For any VirtualAlloc calls that are transient, so you do want them to be freed sometime during the run of the program (perhaps because they happened repeatedly for temporary operations, etc.), then you have to manually call VirtualFree.

- Casey
So if I understood correctly, freeing is required for memory which will be only used in some period of a process.
Thank you all for the answers! I learn so many things here that I didn't get from college.
hacompu
So if I understood correctly, freeing is required for memory which will be only used in some period of a process.

Not exactly. Freeing is required for memory which needs to be freed at some point before the process exits.

If that sounds like a tautology, it is.