Welcome! Log In Create A New Profile

Advanced

SDL ttf question

Posted by andersers 
SDL ttf question
July 26, 2009 07:11PM
Hi

I have made a simple application, that counts how many times you press the A button, and writes it on the TV using sdl and sdl_ttf . Unfortunetly it only works for about 3 minutes, then SDL writes:

Quote

SDL_SetError: Out of memory
SDL_SetError: SDL_UpperBlit: passed a NULL surface

Read source here

I would like to know what I have done wrong.

Thank you
Re: SDL ttf question
July 26, 2009 08:01PM
Probably done nothing wrong. But don't rely on a surface to be created, it can sometimes fail. Try this :

scoret = 0;
scoret = TTF_RenderText_Solid( font, buffer, textColor );
if (scoret) apply_surface( 200, 20, scoret, screen );

Re: SDL ttf question
July 26, 2009 08:20PM
Thank you very much, it helped somewhat. Now SDL only writes
Quote

SDL_SetError: Out of memory
Re: SDL ttf question
July 26, 2009 09:08PM
Obviously there is a memory leak somewhere. I don't know the API, but to me it looks like you keep getting handles/pointers from TTF_RenderText_Solid, but you are never releasing them.



Edited 1 time(s). Last edit at 07/26/2009 09:10PM by henke37.
Re: SDL ttf question
July 26, 2009 09:17PM
Strange, it does not look like memory is leaking. I've used TTF_RenderText_Blended before and there does not seem any problems with it. Try :
scoret = 0;
scoret = TTF_RenderText_Blended( font, buffer, textColor );
if (scoret) apply_surface( 200, 20, scoret, screen );

Also what SDL version are you using?
Re: SDL ttf question
July 26, 2009 09:48PM
I'm using sdl version
Quote

http://sdl-wii.googlecode.com/files/SDL%20Wii%2005-13-2009.zip
http://sdl-wii.googlecode.com/files/SDL%20Wii%2005-13-2009.zip
05-13-2009

TTF_RenderText_Blended( font, buffer, textColor );
gives the same error.
Re: SDL ttf question
July 26, 2009 10:20PM
The only thing different I do in my projects is initialize SDL to 24bit. I've never tried TTF with 16bit.
Re: SDL ttf question
July 26, 2009 10:29PM
by "initialize SDL to 24bit" you mean the bitsperpixel in SDL_SetVideoMode, right?

I tried changing the value to 24, but it still fails.
Re: SDL ttf question
July 27, 2009 04:13PM
I'm not sure what the issue is. Unless there's a leak I'm not seeing your code should be working fine.

My only other suggestion would be to use the latest SVN SDL version (that's what I use). It just means you'll have to get and build the libogc SVN, replace the libraries in the libogc dir with the new ones, get the SDL SVN build that and replace the SDL libs.
Re: SDL ttf question
July 27, 2009 04:42PM
scoret is a SDL_Surface pointer which obviously is allocated when you call the TTF_ function
from what I remember, SDL surfaces are internally allocated using SDL_CreateRGBSurface (former SDL_AllocSurface) which itself do some malloc (s)
Obviously, if you are calling this in a loop without freeing the surface, you will most likely run out of memory sooner or later. This is probably what's happening here



Edited 1 time(s). Last edit at 07/27/2009 04:44PM by ekeeke.
Re: SDL ttf question
July 27, 2009 04:43PM
A quick google on the function quickly explains that it returns a pointer to a new surface. And the surface is not freed for you. And you are not freeing it either. That's what I call an obvious memory leak.
Re: SDL ttf question
July 27, 2009 05:05PM
It seems that you are right ekeeke and henke37. I added
if(scoret){SDL_FreeSurface(scoret);}
in the loop just before the rendering, and now SDL makes no errors :-)

Thank you very much to you all!

By the way, before i read your solution i made my own system that can write 3 digit numbers to the screen. What a waste of time :-P At least it was fun writing, hehe.
Re: SDL ttf question
July 27, 2009 05:10PM
Quote
henke37
A quick google on the function quickly explains that it returns a pointer to a new surface. And the surface is not freed for you. And you are not freeing it either. That's what I call an obvious memory leak.

lol, how did I miss that, nice catch. After looking over my coded I noticed I am calling SDL_FreeSurface (luckily)
Re: SDL ttf question
August 04, 2009 01:51PM
Quote
scanff
The only thing different I do in my projects is initialize SDL to 24bit. I've never tried TTF with 16bit.

I am initializing SDL with 16bit but there is no difference in this with 24bits

Problem is that you estinguish Wii's memory and SDL cannot load aditional data

I made this func:
bool ReloadImage(SDL_Surface* &imageSurface, char* fileName)
{
	SDL_FreeSurface(imageSurface); // free surface if it is not NULL
	imageSurface = NULL;
	imageSurface = load_image(fileName); // load new image - function from SDL tutorial
	return imageSurface == NULL ? false : true;
}
Sorry, only registered users may post in this forum.

Click here to login