【发布时间】:2012-09-30 09:16:31
【问题描述】:
这个函数工作得很好,或者编译器/调试器告诉我
void GUIManager::init(ToScreen* tS)
{
toScreen = tS;
loadFonts();
GUI_Surface = SDL_SetVideoMode( toScreen->width, toScreen->height, 32, SDL_SWSURFACE );
components.push_back(&PlainText("Hello, World!", font, -20, -40));
}
在这里,第一个函数调用引发了访问冲突错误。 调试器没有显示任何问题。 我没有机会调试组件[0],因为程序在这里停止。
void GUIManager::draw()
{
// This line here is the problem
components[0].draw(GUI_Surface);
// This line here is the problem
SDL_BlitSurface(GUI_Surface, NULL, toScreen->Screen_Surface, NULL);
}
如果需要,这是我的“组件”
boost::ptr_vector<GUIComponent> components;
如果需要任何其他代码,请告诉我。也许是 PlainText 或 GUIComponent
【问题讨论】:
-
在drwaing之前检查
components中的元素数量是否不为零。 -
组件[0].position.x += 1;通过就好了。
-
您正在将局部变量的地址推送到组件中。鉴于此内存已被释放,以后对它的任何访问都可能导致访问冲突。
标签: c++ inheritance boost sdl