SDL_CreateTextureFromSurface memory leak?

I’m working on my first real game (I’ve created little tests before but nothing actually meant to be played) and this is my first time having to deal with GUI.
I created two classes. GUIHandler and GUI (to be inherited)

Here’s the code I have for GUIHandler

Code:
bool GUIHandler::renderGUI(){
SDL_Surface * message = NULL;
SDL_Texture * texture = NULL;

SDL_Rect rect;

textField field;

for (int i = 0; i < basic.getNumTextFields(); i++){
    basic.getTextField(i, field);

    message = TTF_RenderText_Solid(font, field.text.c_str(), colour);
    texture = SDL_CreateTextureFromSurface(renderer, message);

    rect.x = field.posX;
    rect.y = field.posY;
    rect.w = message->w;
    rect.h = message->h;

    SDL_RenderCopy(renderer, texture, NULL, &rect);
};

SDL_FreeSurface(message);

return true;

};

While running this, my computer keeps crashing.
After a lot of trouble shooting, I have found out the issue is in SDL_CreateTextureFromSurface. When this is commented out, no crash occurs. I believe this is due to some sort of memory leak associated with SDL_Texture? How can I solve this?

By the way, since this is a GUI, the text is not static or know beforehand (and thus I cannot only create one texture and store it)

2014-05-04 4:30 GMT+02:00 Plazmotech :

I’m working on my first real game (I’ve created little tests before but
nothing actually meant to be played) and this is my first time having to
deal with GUI.
I created two classes. GUIHandler and GUI (to be inherited)

Here’s the code I have for GUIHandler

Code:

bool GUIHandler::renderGUI(){
SDL_Surface * message = NULL;
SDL_Texture * texture = NULL;

SDL_Rect rect;

textField field;

for (int i = 0; i < basic.getNumTextFields(); i++){
    basic.getTextField(i, field);

    message = TTF_RenderText_Solid(font, field.text.c_str(), colour);
    texture = SDL_CreateTextureFromSurface(renderer, message);

    rect.x = field.posX;
    rect.y = field.posY;
    rect.w = message->w;
    rect.h = message->h;

    SDL_RenderCopy(renderer, texture, NULL, &rect);
};

SDL_FreeSurface(message);

return true;

};

While running this, my computer keeps crashing.
After a lot of trouble shooting, I have found out the issue is in
SDL_CreateTextureFromSurface. When this is commented out, no crash occurs.
I believe this is due to some sort of memory leak associated with
SDL_Texture? How can I solve this?

By the way, since this is a GUI, the text is not static or know beforehand
(and thus I cannot only create one texture and store it)

Not sure if it fixes your problem, but there’s a lot of very basic things
wrong with your snippet.
You allocate a surface and texture object in a loop each iteration, but
never free any of the textures,
and only the last allocated surface.
Also, you shouldn’t create/destroy textures on every frame/redraw, that is
very expensive. Keep one
texture (you can reallocate it if your size requirements change) per
graphical widget / whatever,
and just SDL_UpdateTexture new pixels into it.
Oh, and you might want to check for returned null pointers when creating
objects, just in case.

2014-05-04 4:46 GMT+02:00 Jonas Kulla <@Jonas_Kulla>:

2014-05-04 4:30 GMT+02:00 Plazmotech :

I’m working on my first real game (I’ve created little tests before but

nothing actually meant to be played) and this is my first time having to
deal with GUI.
I created two classes. GUIHandler and GUI (to be inherited)

Here’s the code I have for GUIHandler

Code:

bool GUIHandler::renderGUI(){
SDL_Surface * message = NULL;
SDL_Texture * texture = NULL;

SDL_Rect rect;

textField field;

for (int i = 0; i < basic.getNumTextFields(); i++){
    basic.getTextField(i, field);

    message = TTF_RenderText_Solid(font, field.text.c_str(), colour);
    texture = SDL_CreateTextureFromSurface(renderer, message);

    rect.x = field.posX;
    rect.y = field.posY;
    rect.w = message->w;
    rect.h = message->h;

    SDL_RenderCopy(renderer, texture, NULL, &rect);
};

SDL_FreeSurface(message);

return true;

};

While running this, my computer keeps crashing.
After a lot of trouble shooting, I have found out the issue is in
SDL_CreateTextureFromSurface. When this is commented out, no crash occurs.
I believe this is due to some sort of memory leak associated with
SDL_Texture? How can I solve this?

By the way, since this is a GUI, the text is not static or know
beforehand (and thus I cannot only create one texture and store it)

Not sure if it fixes your problem, but there’s a lot of very basic things
wrong with your snippet.
You allocate a surface and texture object in a loop each iteration, but
never free any of the textures,
and only the last allocated surface.
Also, you shouldn’t create/destroy textures on every frame/redraw, that is
very expensive. Keep one
texture (you can reallocate it if your size requirements change) per
graphical widget / whatever,
and just SDL_UpdateTexture new pixels into it.
Oh, and you might want to check for returned null pointers when creating
objects, just in case.

Actually, nevermind the keeping around one texture per widget, this might
be too much hassle for
someone who’s very new to this stuff. Just remember to free everything
you’re allocating.

Jonas Kulla wrote:

2014-05-04 4:46 GMT+02:00 Jonas Kulla <nyocurio at gmail.com (nyocurio at gmail.com)>:

2014-05-04 4:30 GMT+02:00 Plazmotech <@Plazmotech (@Plazmotech)>:

   	I'm working on my first real game (I've created little tests before but nothing actually meant to be played) and this is my first time having to deal with GUI.

I created two classes. GUIHandler and GUI (to be inherited)

Here’s the code I have for GUIHandler

Code:

bool GUIHandler::renderGUI(){

?? ?? SDL_Surface * message = NULL;
?? ?? SDL_Texture * texture = NULL;

?? ?? SDL_Rect rect;

?? ?? textField field;

?? ?? for (int i = 0; i < basic.getNumTextFields(); i++){
?? ?? ?? ?? basic.getTextField(i, field);

?? ?? ?? ?? message = TTF_RenderText_Solid(font, field.text.c_str(), colour);
?? ?? ?? ?? texture = SDL_CreateTextureFromSurface(renderer, message);

?? ?? ?? ?? rect.x = field.posX;
?? ?? ?? ?? rect.y = field.posY;
?? ?? ?? ?? rect.w = message->w;
?? ?? ?? ?? rect.h = message->h;

?? ?? ?? ?? SDL_RenderCopy(renderer, texture, NULL, &rect);
?? ?? };

?? ?? SDL_FreeSurface(message);

?? ?? return true;
};

While running this, my computer keeps crashing.
After a lot of trouble shooting, I have found out the issue is in SDL_CreateTextureFromSurface. When this is commented out, no crash occurs. I believe this is due to some sort of memory leak associated with SDL_Texture? How can I solve this?

By the way, since this is a GUI, the text is not static or know beforehand (and thus I cannot only create one texture and store it)

Not sure if it fixes your problem, but there’s a lot of very basic things wrong with your snippet.
You allocate a surface and texture object in a loop each iteration, but never free any of the textures,
and only the last allocated surface.
Also, you shouldn’t create/destroy textures on every frame/redraw, that is very expensive. Keep one
texture (you can reallocate it if your size requirements change) per graphical widget / whatever,
and just SDL_UpdateTexture new pixels into it.
Oh, and you might want to check for returned null pointers when creating objects, just in case.

Actually, nevermind the keeping around one texture per widget, this might be too much hassle for
someone who’s very new to this stuff. Just remember to free everything you’re allocating.

I’m not new to C++, just SDL. SDL_UpdateTexture might be the solution - I never knew this function existed. I’ll check it out, thanks!