SDL Memory leak when using TTF_RenderText_Blended

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Renderer{
public:
    void text_to_sdl_surface(Text text, Colour colour);
    Font get_font(std::string font_name, int font_size);
private:
    SDL_Surface * text_surface;
};

void Renderer::text_to_sdl_surface(Text text, Colour colour) {
    glColor4f(1,1,1,1);
    SDL_Color text_colour;
    text_colour.r = Uint8(colour.get_colour_as_int()[0]);
    text_colour.g = Uint8(colour.get_colour_as_int()[1]);
    text_colour.b = Uint8(colour.get_colour_as_int()[2]);
    TTF_Font * temp_font = get_font(text.get_fontname(), text.get_fontsize()).get_font();
    text_surface = TTF_RenderText_Blended(temp_font, text.get_string().c_str(), text_colour);

}


I am using SDL_TTF to draw text and have a memory leak that I have narrowed down to the above function, specifically this line:
text_surface = TTF_RenderText_Blended(temp_font, text.get_string().c_str(), text_colour);
After using the surface in opengl I am calling SDL_FreeSurface before the function above is run again however it is still leaking.
If I disable the opengl function that draws the surface to the screen it still leaks, however if I comment out the TTF_RenderText_Blended line it doesn't leak.
Last edited on
text_surface is private so how can you free it?
Thanks for the reply!
Sorry, I missed out information that I didn't think was necessary, here is the full class definition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Renderer{
public:
    Renderer();
    bool is_font_cached(std::string font_name, int font_size);
    void render(std::vector<Shape> shapes_to_render);
    void resize();
    void rasterize(int width, int height);
    void cleanup();
    void update_window();
    void draw_surface();
    void cache_font(std::string font_name, int font_size);
    std::vector<float> cartesian_to_opengl(std::vector<float> cartesian_coordinates);
    std::vector<int> get_window_dimensions();
    Font get_font(std::string font_name, int font_size);
    void text_to_sdl_surface(Text text, Colour colour);
    void draw_sdl_surface(float xpos, float ypos);
    int get_width_of_text(std::string text);
    SDL_Surface * &get_text_surface();
private:
    SDL_Window * window;
    SDL_GLContext glContext;
    std::vector<Font> font_cache;
    SDL_Surface * text_surface;
};


In the render function I call both text_to_sdl_surface and draw_sdl_surface, after that I use SDL_FreeSurface inside of the render function to free the surface:
1
2
3
text_to_sdl_surface(shapes_to_render[i].get_text(), shapes_to_render[i].get_colour());
draw_sdl_surface(shapes_to_render[i].get_coordinates()[0], shapes_to_render[i].get_coordinates()[1]);
SDL_FreeSurface(text_surface);

So text_surface should be accessible.

Once again, sorry for being unclear before and thanks in advance for any help :)
I don't know. It's really hard to guess. Are you sure text_to_sdl_surface is only called in one place?
I only call text_to_sdl_surface in once place in the whole project, which is in that render function :/
How do you know you have a memory leak. If you comment out the call to draw_sdl_surface do you still have a leak?
If the program is left to run it crashes. If i have task manager open when it runs I can watch it's memory use continually rise proportional to the number of calls to text_to_sdl_surface in each cycle of the main loop.
If i comment out the call to draw_sdl_surface it still leaks. :/
Thanks for the help! :)
It looks like you never free the TTF_Font. You have a class called Font so you probably should free the TTF_Font in the Font destructor. If you do that you will also have to make sure to keep the Font object alive for as long as you use the TTF_Font.

1
2
Font font = get_font(text.get_fontname(), text.get_fontsize()).get_font();
text_surface = TTF_RenderText_Blended(font.get_font(), text.get_string().c_str(), text_colour);

For the above to work you will also have to implement proper copy/move semantics for the Font class.
Last edited on
Topic archived. No new replies allowed.