这是我呈现文本的方法(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;
}
}要呈现到屏幕上的字符串的文本看起来很好(不是空/空)。表面(surf_return)为空的原因是什么?
异常消息:
Csurface.cpp 30异常在Game.exe中在0x76DCC42D抛出: Microsoft C++异常: int位于内存位置0x0032E950。
发布于 2015-11-24 17:42:42
我能读到一条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);
}https://stackoverflow.com/questions/33899190
复制相似问题