Search code examples
c++sdltruetype

SDL/TTF: TTF_RenderText_Blended_Wrapped returns NULL


this is my method for rendering a text (file = Csurface.cpp):

    SDL_Surface * Csurface::onLoadText(const char* text, TTF_Font* font, SDL_Color text_color, int length)
    {
        //OutputDebugString("Csurface.cpp onLoadText called\n");
        try
        {
            SDL_Surface *surf_return = NULL;

            surf_return = TTF_RenderText_Blended_Wrapped(font, text, text_color, length);

            if (surf_return != NULL) {
                return surf_return;
            }
            else {
                throw 30;
            }
        }
        catch (int e)
        {
            OutputDebugString("EXCEPTION \n");
            OutputDebugString(("Csurface.cpp "+std::to_string(e)).c_str());
EDIT: ADDED THIS LINE
        OutputDebugString(TTF_GetError());
            return NULL;
        }
    }

When I am trying to render the information from a vector in a loop to the screen the method starts to return NULL after some time. This seems to be after a random number of calls of the method. This occurs only if I try to render text in a loop (file = Capp_OnRender.cpp):

    BOOST_FOREACH(Incredient & incredient_value, v_incredients)
    {
        if (y > G_SCREEN_HEIGHT / 100 * 95) {
            x_buttons = G_SCREEN_WIDTH / 100 * 40;
            x_text = G_SCREEN_WIDTH / 100 * 41;
            y = G_SCREEN_HEIGHT / 100 * 20;
        }
        std::string text_incredient = i_game_logic.get_text_incredient(incredient_value);
        surf_text_incredient = Csurface::onLoadText(text_incredient.c_str(), font_large, text_color, G_SCREEN_WIDTH / 100 * 30);
        if (surf_list_incredients != NULL) {
            y_text = y + G_SCREEN_HEIGHT / 100 * 1;
            Csurface::OnDraw(surf_list_incredients, surf_button_buy, x_buttons, y);
            Csurface::OnDraw(surf_list_incredients, surf_text_incredient, x_text, y_text);
            y += G_SCREEN_HEIGHT / 100 * 7;
        }
        else {
            throw 30;
        }
    }

The text of the string to be rendered to the screen looks fine (not empty/NULL). What could be the reason, that the surface (surf_return) is NULL?

the exception-message:

Csurface.cpp 30 Exception thrown at 0x76DCC42D in Game.exe: Microsoft C++ exception: int at memory location 0x0032E950.


Solution

  • I can read a Out of memory message.

    You have to free surf_text_incredient after using it, it should avoid the memory leak. Like so:

    BOOST_FOREACH(Incredient & incredient_value, v_incredients)
        {
            if (y > G_SCREEN_HEIGHT / 100 * 95) {
                x_buttons = G_SCREEN_WIDTH / 100 * 40;
                x_text = G_SCREEN_WIDTH / 100 * 41;
                y = G_SCREEN_HEIGHT / 100 * 20;
            }
            std::string text_incredient = i_game_logic.get_text_incredient(incredient_value);
            surf_text_incredient = Csurface::onLoadText(text_incredient.c_str(), font_large, text_color, G_SCREEN_WIDTH / 100 * 30);
            if (surf_list_incredients != NULL) {
                y_text = y + G_SCREEN_HEIGHT / 100 * 1;
                Csurface::OnDraw(surf_list_incredients, surf_button_buy, x_buttons, y);
                Csurface::OnDraw(surf_list_incredients, surf_text_incredient, x_text, y_text);
                y += G_SCREEN_HEIGHT / 100 * 7;
            }
            else {
                throw 30;
            }
            SDL_FreeSurface(surf_text_incredient);
        }