我有一个为我管理SDL_Surface的类,我想让它不必担心释放表面。我有这样的代码至今:C++析构函数早期调用
//In Entity.h
class Entity
{
public:
int x, y, width, height;
Entity(std::string);
~Entity();
void render();
private:
SDL_Surface* texture;
};
//In Entity.cpp
Entity::~Entity()
{
printf("Destroying an Entity");
SDL_FreeSurface(texture);
}
//In main.cpp
Entity puppy("puppy.bmp");
void render() {
clearScreen();
puppy.render();
}
int main (int argc, char **argv) {
printf("started");
init();
bool done = false;
while(!done)
{
...
// Draw the screen
printf("About to render");
render();
SDL_Flip(screen);
}
SDL_Quit();
return 0;
}
当我运行它,我得到Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000260 0x000000010002fd9b in SDL_DisplayFormat()
我是在假设析构函数是越来越早调用,它试图呈现一个空的纹理是否正确?代码工作正常,如果我没有析构函数,只是浪费内存。我也尝试过将它作为一个局部变量,并将它传递给渲染,但这并没有帮助。
编辑:代码运行只有一秒,然后崩溃。它不会等待退出循环。另外,使用gdb单步执行程序会让小狗因为某种原因至少被绘制一次。我上传的完整源在这里:http://dl.getdropbox.com/u/2223161/sdlgame.zip
您似乎没有进行任何SDL初始化。 – 2012-02-18 00:41:26
这看起来不像你的代码,因为它甚至不会编译(或链接,就此而言)。在_你的代码中,你确定没有“实体”的副本吗? – wilhelmtell 2012-02-18 00:47:59
@KerrekSB我删除了一些代码,以防止它变得太长。完整的源代码位于http://dl.getdropbox.com/u/2223161/sdlgame.zip – Cole 2012-02-18 01:31:42