Handmade Hero » Forums » Code » Back buffer allocation
Joystick
16 posts
#21091 Back buffer allocation
2 weeks, 6 days ago Edited by Joystick on May 26, 2019, 9:35 p.m. 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.").
mmozeiko
Mārtiņš Možeiko
1934 posts / 1 project
#21092 Back buffer allocation
2 weeks, 6 days ago Edited by Mārtiņš Možeiko on May 26, 2019, 11:27 p.m.

That is correct. But in Day 4 CreateDIBSection is removed completely. HBITMAP it returned was never used anyway.
Joystick
16 posts
#21094 Back buffer allocation
2 weeks, 6 days ago

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.
mrmixer
Simon Anciaux
616 posts
#21097 Back buffer allocation
2 weeks, 5 days ago Edited by Simon Anciaux on May 27, 2019, 7:54 p.m. 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.