Handmade Hero»Forums»Code
23 posts
Back buffer allocation
Edited by Joystick on Reason: Initial post
Yo homies!
At the very beginning of Handmade Hero, Casey allocates some memory for his back buffer using VirtualAlloc() which he then passes to CreateDIBSection() in order to create a DIB. After spending sometime debugging his code, I've found out that the VirtualFree() function fails and doesn't free up the back buffer. Correct me if I'm wrong but I think the reason for that is because CreateDIBSection() actually changes the address of the allocated buffer and this new address is what being passed to VirtualFree() instead of the old one that was generated by VirtualAlloc(), therefore - causing it to fail.
To make a long story short, I've managed to solve this problem be getting rid of VirtualAlloc() and VirtualFree(), and instead, pass an unallocated buffer straight to CreateDIBSection() and freeing it up later on using DeleteObject().
My question to you is: Is it even necessary to make any sort of allocation for the buffer prior to using CreateDIBSection(), or does this function take care of it for me?
(The doc for CreateDIBSection() states that "If hSection is NULL, the system allocates memory for the DIB.").
Mārtiņš Možeiko
2561 posts / 2 projects
Back buffer allocation
Edited by Mārtiņš Možeiko on
That is correct. But in Day 4 CreateDIBSection is removed completely. HBITMAP it returned was never used anyway.
23 posts
Back buffer allocation
That is correct. But in Day 4 CreateDIBSection is removed completely. HBITMAP it returned was never used anyway.


Oh I see. Thanks for clarifying it for me!
While we're on the topic, I have another small question for you regarding CreateDIBSection(). If a DIBSection is independent of any device, why does it need a device context?
I've tried to pass NULL for the device context and the program worked just the same.
Simon Anciaux
1340 posts
Back buffer allocation
Edited by Simon Anciaux on Reason: typo
I may be wrong, but I think that it's called device independent because in the past it allowed you to use a simple RGB format everywhere and let the Windows API convert it to the format the device needed (for example using color palette). So you still need the device context for the API to know how to do the conversion.