SDL_FreeSurface memory problem

Hello guys,

I have a very strange problem with freeing the memory taken by surfaces in SDL. I have a program in Linux C which is displaying different information screens - every single function is a another screen, the main() function only fires the desired function.

One of the functions:

Code:

void showMessageE1() {
SDL_Surface *message1 = NULL;
SDL_Surface *message2 = NULL;

font = TTF_OpenFont("/etc/sterd_gui/assets/font.ttf", 32);

SDL_FillRect(screen, &clipRect, 0xFF);
SDL_UpdateRect(screen, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

SDL_Rect offset1, offset2;
offset1.x = 20;
offset1.y = 95;

offset2.x = 29;
offset2.y = 135;

message1 = TTF_RenderUTF8_Shaded(font, "BLA BLA BLA", redColor, whiteColor);
message2 = TTF_RenderUTF8_Shaded(font, "IMPORTANT MESSAGE", redColor, whiteColor);

SDL_BlitSurface(message1, NULL, screen, &offset1);
SDL_BlitSurface(message2, NULL, screen, &offset2);

SDL_Flip(screen);

SDL_FreeSurface(message1);
SDL_FreeSurface(message2);

font = NULL;
message1 = NULL;
message2 = NULL;

}

When I switch back and forth between the different screens, the memory keeps increasing (I check it with the ‘top’ command under linux).

I don’t know why the SDL_FreeSurface and NULL assignment to the specific surface, is not freeing the memory at all. Am I doing something wrong or maybe is this a kind of bug? I use also the SDL TTF library (as you can see in the code above) - maybe this is the reason why the memory keeps leaking?

Thanks in advance!

Maybe a little TTF_CloseFont() would help? Other than that, I don’t see
it. I’d need more code.

Jonny DOn Sun, Feb 19, 2012 at 5:01 AM, kidsday <jakub.kania at gmail.com> wrote:

**
Hello guys,

I have a very strange problem with freeing the memory taken by surfaces in
SDL. I have a program in Linux C which is displaying different information
screens - every single function is a another screen, the main() function
only fires the desired function.

One of the functions:

Code:

void showMessageE1() {
SDL_Surface *message1 = NULL;
SDL_Surface *message2 = NULL;

font = TTF_OpenFont("/etc/sterd_gui/assets/font.ttf", 32);

SDL_FillRect(screen, &clipRect, 0xFF);
SDL_UpdateRect(screen, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

SDL_Rect offset1, offset2;
offset1.x = 20;
offset1.y = 95;

offset2.x = 29;
offset2.y = 135;

message1 = TTF_RenderUTF8_Shaded(font, “BLA BLA BLA”, redColor,
whiteColor);
message2 = TTF_RenderUTF8_Shaded(font, “IMPORTANT MESSAGE”, redColor,
whiteColor);

SDL_BlitSurface(message1, NULL, screen, &offset1);
SDL_BlitSurface(message2, NULL, screen, &offset2);

SDL_Flip(screen);

SDL_FreeSurface(message1);
SDL_FreeSurface(message2);

font = NULL;
message1 = NULL;
message2 = NULL;
}

When I switch back and forth between the different screens, the memory
keeps increasing (I check it with the ‘top’ command under linux).

I don’t know why the SDL_FreeSurface and NULL assignment to the specific
surface, is not freeing the memory at all. Am I doing something wrong or
maybe is this a kind of bug? I use also the SDL TTF library (as you can see
in the code above) - maybe this is the reason why the memory keeps leaking?

Thanks in advance!


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You generally don’t want to be hitting the disk just to display a message.
Another idea is to structure the application such that the font is
available when needed, and de-allocated during shutdown.

– rip-offOn 20 February 2012 02:55, Jonathan Dearborn wrote:

Maybe a little TTF_CloseFont() would help? Other than that, I don’t see
it. I’d need more code.

Jonny D

If you’re just using the same font with the same size every time, then only open it once and free it when you’re done:

Code:
void initFont() {
font = TTF_OpenFont("/etc/sterd_gui/assets/font.ttf", 32);
}

void closeFont() {
TTF_CloseFont(font);
}

void showMessageE1() {
SDL_Surface *message1 = NULL;
SDL_Surface *message2 = NULL;

// We don’t need to open the font, since we loaded it earlier
// using initFont()

SDL_FillRect(screen, &clipRect, 0xFF);
SDL_UpdateRect(screen, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

SDL_Rect offset1, offset2;
offset1.x = 20;
offset1.y = 95;

offset2.x = 29;
offset2.y = 135;

message1 = TTF_RenderUTF8_Shaded(font, “BLA BLA BLA”, redColor, whiteColor);
message2 = TTF_RenderUTF8_Shaded(font, “IMPORTANT MESSAGE”, redColor, whiteColor);

SDL_BlitSurface(message1, NULL, screen, &offset1);
SDL_BlitSurface(message2, NULL, screen, &offset2);

SDL_Flip(screen);

SDL_FreeSurface(message1);
SDL_FreeSurface(message2);

// We don’t want to NULL the font either, since we might not be done with it
// we’ll do this in closeFont()
message1 = NULL;
message2 = NULL;
}

Call the initFont() once when you’re program starts, and closeFont() when your program terminates. You’ll also need to call TTF_Quit() after you’ve closed all of your fonts.

Don’t forget to check that the font loaded correctly before using it!------------------------
The Legend of Edgar. A 2D platformer for Windows and Linux. (http://www.parallelrealities.co.uk/p/legend-of-edgar.html)