• Advertisement
Sign in to follow this  

Memory Leak?

This topic is 4182 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

When I run my SDL program (I didn't post this in SDL because that information isn't really relevant), and I look in the Windows Task Manager, The amount of Pagefile usage goes up as long as it's running. When I close it, it goes back to normal. Does this mean anything? Also, i'm using Code::Blocks. Is there anyway to check for a memory leak using the debugger?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Uphoreum
When I close it, it goes back to normal. Does this mean anything?


Sounds indeed like a memory leak.

Quote:
Is there anyway to check for a memory leak using the debugger?


Alas no. You need to either use a leak checker (I am not aware of free ones that run under Windows, sorry), or hack up operator new and operator delete to report leaked memory at program exit.

Share this post


Link to post
Share on other sites
Hmm... I just noticed that it's been happening on other games I've made. When it reaches about 900MB, it freezes up and I can hardly move the mouse. It goes right back down when I close the program though.

Here is the source code to a game that has this problem

Sorry to give a big chunk of code, but I don't know what else to do. I'll do some testing however.

Share this post


Link to post
Share on other sites
Have you tried using the standard <deque> header rather than <deque.h>?

Share this post


Link to post
Share on other sites
Well, I don't think that has anything to do with it, because the other programs don't use it. But I think I found where the problem is happening. It seems to happen when I'm drawing graphics. I took all the graphic drawing funtions out of a problem program and pagefile usage was steady. I just don't see what I'm doing wrong?

Share this post


Link to post
Share on other sites
Also, when I was running this program, I didn't go past the main menu function, which means the problem happens in there too, so the code is now narrowed down to:

void MainMenu()
{
while(run)
{
ClrScreen();
SDL_Color WHITE = {255,255,255};
txt = TTF_RenderText_Solid(menuFont, "Old Skool Cobra",WHITE);
DrawSprite(txt,buffer,40,30);
txt = TTF_RenderText_Solid(menuFont, "Press Any Key", WHITE);
DrawSprite(txt,buffer,45,120);
txt = TTF_RenderText_Solid(menuFont, "To Continue.", WHITE);
DrawSprite(txt,buffer,55,180);
SDL_Flip(buffer);

SDL_Event event;

//Test For Events
while(SDL_PollEvent(&event)) {
switch( event.type ) {
case SDL_QUIT:
run = false;
break;
case SDL_KEYDOWN:
Game();
break;
}
}
}
return;
}
[/SOURCE

Share this post


Link to post
Share on other sites
also:

void DrawSprite(SDL_Surface *surface, SDL_Surface *targetSurface, int x, int y)
{
if(SDL_MUSTLOCK(targetSurface))
{
SDL_LockSurface(targetSurface);
}

//Create Rectangle For Coordinates
SDL_Rect destination;

//Give Rectangle Coordinates
destination.x = x;
destination.y = y;

//Blit Image to Buffer
SDL_BlitSurface( surface, NULL, targetSurface, &destination );

if(SDL_MUSTLOCK(targetSurface))
{
SDL_UnlockSurface(targetSurface);
}
}

Share this post


Link to post
Share on other sites
Odds are that TTF_RenderText_Solid allocates a new graphics entity every time you call it. That would indeed be a leak. Take the calls out of the loop, use a separate pointer for each chunk of text you need to render, and look for the appropriate deallocation routine in the library.

Share this post


Link to post
Share on other sites
Checking the documentation confirms that it indeed is the case:

Quote:
Returns: a pointer to a new SDL_Surface. NULL is returned on errors


Use SDL_FreeSurface to release the surface once you no longer need it.

Share this post


Link to post
Share on other sites
If you do, I wouldn't know :-/ lol. Come to think of it, both problem programs use that function. I'll take it out of the other one and see if that's it.

Share this post


Link to post
Share on other sites
Yes! That would be the problem! Thanks for fixing that! I can finally go to sleep now :-D

Will Pagefile always show problems if a program has a memory leak? I'm just wondering if I could use that as a test for my programs to see if they're causing problems.

PS. I fixed it by calling SDL_FreeSurface(txt) before modifying txt. I guess it was leaving "footprints".

Share this post


Link to post
Share on other sites
Quote:
Original post by Uphoreum
Will Pagefile always show problems if a program has a memory leak?


Depends on how severe the leak is. Memory usage going up when it shouldn't is the telltale sign, but if the leak is a mere trickle, it might take a while for it to show up, and it may be shadowed by other larger allocations/deallocations.

A leak checker really is the best solution. Barring that, a leak-tracking allocator can be very helpful.

Quote:
PS. I fixed it by calling SDL_FreeSurface(txt) before modifying txt. I guess it was leaving "footprints".


You can get faster code by hoisting the render call out of the loop, keeping (and referencing) distinct surfaces, and freeing them once the loop exits. That way you just reuse the same surfaces without having to render them every time (you just blit them). The tradeoff is that you keep more surfaces around, which increases the total memory used by your program, but you can probably afford them if you don't leak. [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement