这是我渲染文本的方法(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;
}
}
当我尝试将循环中的矢量信息渲染到屏幕时,该方法在一段时间后开始返回NULL。这似乎是在该方法的随机调用次数之后。只有当我尝试在循环中呈现文本时才会发生这种情况(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;
}
}
要呈现给屏幕的字符串文本看起来很好(不是空/ NULL)。可能是表面(surf_return)是NULL的原因是什么?
异常消息:
Csurface.cpp 30 Game.exe中的0x76DCC42D抛出异常:Microsoft C ++异常:int在内存位置0x0032E950。
答案 0 :(得分:0)
我可以阅读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);
}