【问题标题】:SDL/TTF: TTF_RenderText_Blended_Wrapped returns NULLSDL/TTF:TTF_RenderText_Blended_Wrapped 返回 NULL
【发布时间】:2015-11-24 16:41:47
【问题描述】:

这是我渲染文本的方法(文件 = 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;
        }
    }

当我尝试将信息从循环中的向量渲染到屏幕时,该方法会在一段时间后开始返回 NULL。这似乎是在随机调用该方法之后。仅当我尝试在循环中呈现文本时才会发生这种情况(文件 = 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;
        }
    }

要呈现到屏幕上的字符串文本看起来很好(非空/NULL)。表面 (surf_return) 为 NULL 的原因可能是什么?

异常消息:

Csurface.cpp 30 在 Game.exe 中的 0x76DCC42D 处引发异常:Microsoft C++ 异常:内存位置 0x0032E950 处的 int。

【问题讨论】:

  • printf("Error : %s", TTF_GetError()); 的输出是什么?
  • 你能在TTF_RenderText_Blended_Wrapped之后添加上一行吗?
  • 对不起,我花了一些时间来重现错误;我现在得到:“在 Game.exe 中的 0x76DCC42D 处引发异常:Microsoft C++ 异常:内存位置 0x0034E8D8 处的 int。Game.exe 中 0x76DCC42D 处引发的异常 Csurface.cpp 30Out of memoryException:Microsoft C++ 异常:内存位置 0x0034EBB4 处的 int。 EXCEPTION Capp_OnRender.cpp 30Exception throw at 0x76DCC42D in Game.exe: Microsoft C++ exception: int at memory location 0x0034E8D8."
  • Capp_OnRender.cpp 是您的文件之一吗?
  • 它是包含调用 onLoadText() 函数的循环的文件

标签: c++ sdl truetype


【解决方案1】:

我可以阅读Out of memory 消息。

surf_text_incredient 使用后必须释放它,它应该避免内存泄漏。像这样:

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);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2020-06-19
    • 2011-10-15
    相关资源
    最近更新 更多