首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SDL/TTF: TTF_RenderText_Blended_Wrapped返回NULL

SDL/TTF: TTF_RenderText_Blended_Wrapped返回NULL
EN

Stack Overflow用户
提问于 2015-11-24 16:41:47
回答 1查看 2.5K关注 0票数 1

这是我呈现文本的方法(file = Csurface.cpp):

代码语言:javascript
复制
    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。这似乎是在该方法的随机调用次数之后。只有当我尝试在循环中呈现文本(file =Capp_OnRender.cpp)时,才会发生这种情况:

代码语言:javascript
复制
    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;
        }
    }

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

异常消息:

Csurface.cpp 30异常在Game.exe中在0x76DCC42D抛出: Microsoft C++异常: int位于内存位置0x0032E950。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-24 17:42:42

我能读到一条Out of memory信息。

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

代码语言:javascript
复制
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);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33899190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档