Jump to content
  • Advertisement

Archived

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

Peon

Memory Problem

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, my pong clone is finished - mostly. Problem is, it crashes my computer forcing a manual restart after about 5 minutes of play. I wanted to get to the bottom of this so I checked my system resources and watched it as I played. To my horror, every time a goal was scored, a whopping 15% of the system and GDI resources disappeared! As soon as I exited, they returned to the usual. So what''s up here? I could probably have Unreal, and ICQ, tons of thing running in the background and have mnore resources than with my pong game? Any suggestions? If it helps, my pong clone is a WIN32 app and uses GDI to do graphics. Could the problem be playing a wave file when a goal is scored? Thanks, Peon

Share this post


Link to post
Share on other sites
Advertisement
Well, I would have to see the code to point out the specific problem, but what you are seeing is most likely a GDI memory leak. Basically you are allocating device contexts that you aren''t de-allocating when you''re done with it (fortunately the system is picking it up and taking care of it for you when the app exits.)

You might consider checking for device contexts that you create (CreateDC and the like) and fail to release properly (ReleaseDC and DeleteDC). Windows does have a finite pool of device context space, and if you run out it causes Bad Things (TM).

It most likely isn''t your sounds if you''re eating GDI resources btw... GDI stands for Graphics Device Interface, it only handles drawing.

-fel

Share this post


Link to post
Share on other sites
OK, I figured as much. I double checked all my code but still, a leak somewhere. I finally commented out parts of my GameMain() so that only one function would execute. I wasn''t surprised when the problem continued. Here is the function that I tested:

  int DrawScreen()
{

// draw the scores


HDC hdc = GetDC(hwnd);

SetTextColor(hdc, RGB(255,255,255));
SetBkColor(hdc, RGB(0,0,0));
SetBkMode(hdc, TRANSPARENT);

sprintf(buffer, "%d", iPlayer1Goals);
TextOut(hdc, 100, 20, buffer, strlen(buffer));

sprintf(buffer, "%d", iPlayer2Goals);
TextOut(hdc, 300, 20, buffer, strlen(buffer));

// Draw the logo


SetTextColor(hdc, RGB(255,255,255));
SetBkMode(hdc, TRANSPARENT);

TextOut(hdc, 0, 0, "B A T T L E - S T Y X v1.0", strlen("B A T T L E - S T Y X v1.0"));

// create a pen to illustrate actual play area


HPEN white_pen = CreatePen(PS_SOLID, 1, RGB(255,255,255));

SelectObject(hdc, white_pen);

MoveToEx(hdc, 0,40, NULL);

LineTo(hdc, 400,40);

DeleteObject(white_pen);

ReleaseDC(hwnd, hdc);

return(0);

}


Looks OK, no? The only other that is "going" is WinProc, maybe the problem is there? FYI, I lose about 15% of the GDI resources every 5-10 seconds -- BAD!

Peon

Share this post


Link to post
Share on other sites
Hi Peon,

I think the problem is that you cannot delete objects when they are selected into device contexts. So you have to select them out first. Try this:

  
HPEN *old = SelectObject(hdc, white_pen);
MoveToEx(hdc, 0,40, NULL);
LineTo(hdc, 400,40);
SelectObject(hdc,old)
DeleteObject(white_pen);


I''m not sure how efficient it is to continually be creating and deleting the pens, so you may consider making white_pen a global (or static) and only deleting it at destruction time. But perhaps this is a negligeable overhead.

Good luck!

Share this post


Link to post
Share on other sites
quote:
I''m not sure how efficient it is to continually be creating and deleting the pens, so you may consider making white_pen a global (or static) and only deleting it at destruction time. But perhaps this is a negligeable overhead.




Well yeah, it''s a pong game - speed is not an issue I''ll remember that though, I had no idea. I''ll go try out what you said. Please work!

Share this post


Link to post
Share on other sites
I had this problem a while ago.. and I think the problem was creating too many objects and deleting them each frame.
Make all your HGDIOBJ(HPEN, HBRUSH, etc) objects static or global, initialize them once and destroy them once.
This should fix it.
And you don''t really need to be calling GetDC and ReleaseDC every frame, you can call it once at startup and once at shutdown if you like.

Share this post


Link to post
Share on other sites
When you select your pen into the window DC you need to save the result (i.e. the old pen), and then select the old pen back before you release the DC.

Share this post


Link to post
Share on other sites
I ended up making my pens global, which solved the problem. The Get, Release is fine for now but you''re right Quantum, I tried what you said and it works nicely as well (better for speed, I assume) Newbies take heed - TOTWPGs is a great book, but be careful because not EVERYTHING is explained Thanks all

Peon

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!