SDL/TTF: TTF_RenderText_Blended_Wrapped returns 空
SDL/TTF: TTF_RenderText_Blended_Wrapped returns NULL
这是我渲染文本的方法(文件 = 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;
}
}
当我尝试将信息从循环中的矢量呈现到屏幕时,该方法在一段时间后开始为 return 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;
}
}
要呈现到屏幕上的字符串文本看起来不错(不是 empty/NULL)。表面 (surf_return) 为 NULL 的原因可能是什么?
异常消息:
Csurface.cpp 30 Exception thrown at 0x76DCC42D in Game.exe: Microsoft C++ exception: int at memory location 0x0032E950.
我可以阅读 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);
}
这是我渲染文本的方法(文件 = 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;
}
}
当我尝试将信息从循环中的矢量呈现到屏幕时,该方法在一段时间后开始为 return 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;
}
}
要呈现到屏幕上的字符串文本看起来不错(不是 empty/NULL)。表面 (surf_return) 为 NULL 的原因可能是什么?
异常消息:
Csurface.cpp 30 Exception thrown at 0x76DCC42D in Game.exe: Microsoft C++ exception: int at memory location 0x0032E950.
我可以阅读 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);
}