Jump to content
  • Advertisement
Sign in to follow this  
TheHighPriestess

RAM Leak - SDL & SDL_ttf

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

Hi! I am working on a rather simple game, and the project is pretty much finished. I have been following the amount of RAM the process uses periodically throughout developing this game. Thus, I when I encountered a nasty RAM leak, I was able to corner the problem immediately. Normally my program uses 3.3MB total, but with this code to print out a score and the clock, it will grow each time the screen is drawn. This is the code. clock_text and kills_text are both defined globally as follows: SDL_Surface *kills_text; SDL_Surface *clock_text; and are called in a seperate function as so:
int GamePaint(SDL_Surface* screen)

{
	char dummy[50];
	SDL_Rect offset;
	SDL_Color textColor = { 0xDE, 0x30, 0xBA  };


	//Get offsets

	offset.x = 0;

	offset.y = 440; 

	if (_iFlash < 1)

	{

		SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x64, 0xFF ) );

		// Draw the sprites

		DrawSprites(screen);

	}

	else

	{

		SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0x00, 0x00 ) );

		--_iFlash;

	}


	SDL_BlitSurface(panel, NULL, screen, &offset );


	sprintf(dummy, "%02d:%02d:%02d", myClock.hours, myClock.minutes, myClock.seconds);
	clock_text = TTF_RenderText_Solid( font, dummy, textColor);
	sprintf(dummy, "%i", _iScore);
	kills_text = TTF_RenderText_Solid( font, dummy, textColor);

	offset.x = 60;

	offset.y = 441; 
	SDL_BlitSurface(kills_text, NULL, screen, &offset );
	offset.x = 350;

	offset.y = 441; 
	SDL_BlitSurface(clock_text, NULL, screen, &offset );


	return 0;



}

Now, originally these surfaces were defined in the same function, but I then realized I was going to have to allocate and release each structure for every paint, which seemed very inefficient. I think it's obvious that the clock_text surface is being allocated dynamically at each function call to TTF_RenderText_Solid, but I don't want that to be the case, but rather have the image drawn over. I originally had the surfaces set to NULL before this code, but clearly that's not necessary with this bug. I've tried a couple things, but will research the functions I'm using more to see if I can't solve this on my own.

Share this post


Link to post
Share on other sites
Advertisement
If TTF_RenderText_Solid() allocates a new surface everytime it's called there's nothing you can do about it except of changing SDL_ttf itself. So, for now just calling SDL_FreeSurface() on those two surfaces should prevent that memory leak. And if your game ran fast enough till now that shouldn't change much. As SDL_ttf is already allocation memory there and freeing it shoulden't hit the performance that much.

Share this post


Link to post
Share on other sites
Quote:
Original post by sebbit
If TTF_RenderText_Solid() allocates a new surface everytime it's called there's nothing you can do about it except of changing SDL_ttf itself. So, for now just calling SDL_FreeSurface() on those two surfaces should prevent that memory leak. And if your game ran fast enough till now that shouldn't change much. As SDL_ttf is already allocation memory there and freeing it shoulden't hit the performance that much.


Hi and thanks for your reply.

That's indeed what I did to solve the problem. It seems inefficient but you're right, the performance is still satisfactory. I will meditate further on a better way to do my text drawing, in the case that in the future I have to draw enough text that performance may be a bigger concern. I'll also take a look at the library code to see if it doesn't provide some insight or maybe I can make or find a new function that does exactly what I want. It's open source so I wouldn't mind helping code it if I can.

Share this post


Link to post
Share on other sites
You could change your code so that the "score" text only changes when the score does. This would lower the overall number of allocations and deallocations. You could do something similar with the clock, remember if you are running at 60 frames per second then you will be displaying the same clock value 60 times.

Share this post


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

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!