Here's the (relevant) code for my pro::surface class:
/** Wraps up SDL_Surface* **/ class surface { SDL_Surface* _surf; public: /** Constructor. ** @param surf an SDL_Surface pointer. **/ surface(SDL_Surface*); /** Overloaded = operator. **/ void operator = (SDL_Surface*); /** calls SDL_FreeSurface(). **/ void free(); /** destructor. Also free()s the internal SDL_Surface. **/ virtual ~surface(); } Now the problem is that in my main function, the object destroys itself (and hence calls the destructor which dangerously free()s the SDL Video Surface!) before the real rendering begins.
int main(int argc, char** argv) { ... // declared here pro::surface screen = SDL_SetVideoMode(320,240,16,SDL_HWSURFACE|SDL_DOUBLEBUF); // event-handling done here, but the Video Surface is already freed! while(!done) { ... } // note that "screen" is not used in this loop. // hence, runtime error here. SDL_Quit() tries to free() the Video Surface again. SDL_Quit(); return 0; } So my question is, is there any way to stop the pro::surface instance from destroying itself before the program ends? Doing memory management manually works though:
/* this works, since I control the destruction of the object */ pro::surface* screen = new pro::surface( SDL_SetVideoMode(..) ); /* it destroys itself only when **I** tell it to! Muhahaha! */ delete screen; /* ^ but this solution uses pointer (ewww! I hate pointers) */ But isn't there a better way, without resorting to pointers? Perhaps some way to tell the stack to not delete my object just yet?
pro::surface"yours"? I don't know about SDL, is it possible to hold theSDL_Surfacein a shared pointer instead of a raw pointer? Or at least something with shared pointer semantics?