Public Group

Archived

This topic is now archived and is closed to further replies.

Double Buffering

This topic is 5557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Got a question about double buffering and making sure I don''t have a memory leak or anything like that. I''ve never done back buffering before, and I want to do it within an instantiation of a class. I have a class, SpaceShip, and the class handles all of the drawing itself (this is GDI).
// Constructor definition
SpaceShip(HWND hwnd, HDC hBBufferDC);

So in the driver for my class (just testing it before I build the rest of the game), I have code like this to set up double buffering(before the game loop).
// set up double buffering

HDC hdc = GetDC(hwnd);
HDC hBufferDC = CreateCompatibleDC(hdc);
RECT r;
GetClientWindow(hwnd,r);
int xSize = r.right - r.left;
int ySize = r.bottom - r.top;
HBITMAP hBufferBitmap = CreateCompatibleCompatible(hBufferDC, xSize, ySize);
HBITMAP hOldBufferBitmap = (HBITMAP)SelectObject(hBufferDC, hBufferBitmap);

// instatiate the spaceship

SpaceShip spaceShip(hwnd, hBufferDC);

then within my game loop I have something like this
spaceShip.Draw();
BitBlt(hDC, 0, 0, xSize, ySize, hBufferDC, 0, 0, SRCCOPY);

And then just before I exit the program I do some cleanup:
SelectObject(hBufferDC, hOldBufferBmp);
DeleteObject(hBufferBmp);
DeleteDC(hBufferDC);
ReleaseDC(hwnd, hdc);

The above code segments work as I expect them to, I''m just wondering if there is anything I''m doing wrong in terms of standard programming practices or memory management. The way I understand handles they are essentially pointers so when I pass the HDC to my constructor I am working within the memory allocated in my driver. If that''s true I shouldn''t need to do any memory management on the HDC that my class uses. Is that correct? I guess basically I''m wondering if anyone spots some flaw I am overlooking. This is my first windows program, first double buffer, and first game project.

Share on other sites
As far as I can see everything seems to look good. I am in no way the memory management master, but it seems like you are doing cleanup just fine. All you have to remember is that all dynamically declared objects should be removed using delete() or free(). When a game closes, it is not "necessary" to delete all of the objects that were used. The main thing you want to worry about it deleting objects that were temporarily used during runtime. That way, you don''t encounter problems with unavailable memory during runtime. I believe that windows will actually remove all unused objects after a process has terminated. But, it is still good practice to remove objects at the end of your code. Good luck!

Regards,
Mike

Share on other sites
Yah, I guess I was most concerned about whether or not the HDC is truely a pointer or not. What has me unsure of the memory management factor is that the variable is changing scope (well, I believe this is incorrect terminology - but it is created in my WinMain function, and is used within the class SpaceShip and I pass by value).

I guess my concerns are unjustified. Who would have thought I actually knew what I was doing. Thanks!

Share on other sites
Just for clarification, an HDC (and, in general, all Windows HANDLEs) is not a pointer. It''s more of an ID number, and Windows internally keeps a lookup table of all HANDLEs and the resources they point to. So as long as you Delete() your HANDLEs correctly, you won''t have memory leaks -- and in Windows 2000 and later, you don''t even have to delete the HANDLEs, as Windows will take care of it for you. However, it is sloppy not to clean them up, so do it anyways

Share on other sites
quote:
Original post by ApochPiQ
in Windows 2000 and later, you don''t even have to delete the HANDLEs, as Windows will take care of it for you. However, it is sloppy not to clean them up, so do it anyways

I would like to reiterate that just because Windows 2000 and above cleans up for you it is NEVER a good idea to rely on this.

Qui fut tout, et qui ne fut rien

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• Forum Statistics

• Total Topics
632580
• Total Posts
3007189

×