# Unity SDL memory problem

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

## Recommended Posts

Hi. this is my first question in this community. i hope it's in the right the right section. and first of all, i'm sorry if my english is not correct. i've recently started to learn SDL by creating a game while reading a few tutorials. the game development is going pretty well, but i have a big problem. no matter what i do, even if i open the game and just stand still, the memory usage just keeps raising!! i think the most obvious source of the problem are the SDL_Surfaces. after searching a while, i tried to use SDL_FreeSurface every time i loaded a new Surface, but i didn't solve anything. most of the code where i handle Surfaces are in functions that are only executed at the start of each level, so the problem can't be with those, i think. so here's a part of my code that i think may be the problem this code is being constantly executed. it checks the current player position, and draws a message on the screen based on that position:
void drawMessage() {
font = TTF_OpenFont("mainData/fonts/testFont.ttf", 32 );
if (player.x+backgroundOfs.x < gBackground->w/2) {
if (player.y+backgroundOfs.y < gBackground->h/2) {
gMessage = TTF_RenderText_Solid( font, "The Top Left Room", textColor );
}
else
gMessage = TTF_RenderText_Solid( font, "The Bottom Left Room", textColor );
}
else {
if (player.y+backgroundOfs.y < gBackground->h/2) {
gMessage = TTF_RenderText_Solid( font, "The Top Right Room", textColor );
}
else
gMessage = TTF_RenderText_Solid( font, "The Bottom Right Room", textColor );
}
drawSurface(WIDTH/2-gMessage->w/2,50, gMessage, gScreen);
SDL_FreeSurface(gMessage);
}
and here the code that draws the player sprite (the sprite is constantly changed, so this need to be continually executed:
void drawPlayer() {

if (player.movX>1 || player.movX<-1) {
if (playerSpriteTick<30) {
drawSurface(player.x, player.y, player.playerSprites, gScreen, &playerSpriteOfs[2]);
playerSpriteTick++;
}
else if (playerSpriteTick<60) {
drawSurface(player.x, player.y, player.playerSprites, gScreen, &playerSpriteOfs[3]);
playerSpriteTick++;
}
else playerSpriteTick=0;
}
else if (player.movY>1 || player.movY<-1) {
if (playerSpriteTick<30) {
drawSurface(player.x, player.y, player.playerSprites, gScreen, &playerSpriteOfs[0]);
playerSpriteTick++;
}
else if (playerSpriteTick<60) {
drawSurface(player.x, player.y, player.playerSprites, gScreen, &playerSpriteOfs[1]);
playerSpriteTick++;
}
else playerSpriteTick=0;
}
else {
drawSurface(player.x, player.y, player.playerSprites, gScreen, &playerSpriteOfs[1]);
}
}
the SDL_FreeSurface in the end of each code wasn't originally there, but it didn't change anything note that i'm using a custom function to draw the surfaces to screen. here's the function:
void drawSurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip= NULL) {
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;

SDL_BlitSurface(source, clip, destination, &offset);
}
so, could the problem be in this part of the code? what can i do to solve it? thanks in advance

##### Share on other sites
It looks you open the font each time drawPlayer() is called (using TTF_OpenFont()), but you never close it.

##### Share on other sites

TTF_Font* font = TTF_OpenFont();

to:

static TTF_Font* font = TTF_OpenFont();

##### Share on other sites
Quote:
 Original post by ConorHor if you made TTF_Font* font = TTF_OpenFont(); to:static TTF_Font* font = TTF_OpenFont();
Note that even if the pointer to the font object is made static, the font object will still need to be freed at some point.

##### Share on other sites
A better solution would be to create the font once at startup and pass a reference/pointer to the font to the functions that require it and then clean it up when it is no longer needed. This holds true with most resources.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 60
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632967
• Total Posts
3009560
• ### Who's Online (See full list)

There are no registered users currently online

×