Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ destructor called early
    primarykey
    data
    text
    <p>I have a class that manages a SDL_Surface for me, and I want to make it so that I don't have to worry about freeing the surface. I have this code so far:</p> <pre><code>//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; } </code></pre> <p>When I run it, I get <code>Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000260 0x000000010002fd9b in SDL_DisplayFormat ()</code> Am I correct in assuming that the destructor is getting called early and it's trying to render a NULL texture? The code works fine if I don't have the destructor and just waste memory. I've also tried it with puppy being a local variable and passing it to render, but that doesn't help.</p> <p>EDIT: The code runs for only a second, then crashes. It doesn't wait to exit the loop. Also, stepping through the program with gdb reveals the puppy to be drawn at least once for some reason. I've uploaded the full source here: <a href="http://dl.getdropbox.com/u/2223161/sdlgame.zip" rel="nofollow">http://dl.getdropbox.com/u/2223161/sdlgame.zip</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload