Clean Up Code belongs

Is it better to use the atexit( ) function to clean up sdl-based components or
is it better if you put the clean-up code in the destructor (assuming your sdl
is placed in a class)…

As a general principle, whatever you constructed in a constructor should be cleaned up
in the corresponding destructor. It makes the code easier to read and maintain.>----- Original Message ----

From: Armond Sarkisian <armond.sarkisian at gmail.com>
Subject: [SDL] Clean Up Code belongs

Is it better to use the atexit( ) function to clean up sdl-based components or
is it better if you put the clean-up code in the destructor (assuming your sdl
is placed in a class)…

So if you were to throw an exception the destructor would automatically be
called correct?

That would be true in Delphi, which is what I’m most familiar with. I don’t
know if that’s how C++ handles it, though.>----- Original Message ----

From: Armond Sarkisian <armond.sarkisian at gmail.com>
Subject: Re: [SDL] Clean Up Code belongs

So if you were to throw an exception the destructor would automatically be
called correct?

so this leads me to my next question which is would it be alright if SDL_Quit( )
was called twice? What if I have SDL_Quit( ) in my destructor and also in my
error catching routine, would that pose a problem…

if yes then is there a way to check if sdl_quit was called? similarily to
SDL_WasInit but dealing with the quit function rather than the init function.

I believe WasInit will work, since it basically undoes the the subsystem initialization. But again, I’m not completely certain. You’d be better off just finding out if an exception thrown during object creation in C++ automatically calls the destructor or not.

Any C++ coders on here who know the answer to that one?>----- Original Message ----

From: Armond Sarkisian <armond.sarkisian at gmail.com>
Subject: Re: [SDL] Clean Up Code belongs

so this leads me to my next question which is would it be alright if SDL_Quit( )
was called twice? What if I have SDL_Quit( ) in my destructor and also in my
error catching routine, would that pose a problem…

if yes then is there a way to check if sdl_quit was called? similarily to
SDL_WasInit but dealing with the quit function rather than the init function.

Hi,

If the exception is thrown in the constructor, then the destructor won’t
be called. The object was not been
fully initialized.

http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2

If you want to prevent this scenario, either move the initialization
outside of the constructor, or use a catch
in the initializer list, but beware that not all C++ compilers support this.

http://weseetips.com/2008/06/04/how-to-catch-exceptions-from-constructor-initializer-list/

Cheers,
Paulo

Mason Wheeler schrieb:> I believe WasInit will work, since it basically undoes the the subsystem initialization. But again, I’m not completely certain. You’d be better off just finding out if an exception thrown during object creation in C++ automatically calls the destructor or not.

Any C++ coders on here who know the answer to that one?

----- Original Message ----
From: Armond Sarkisian <armond.sarkisian at gmail.com>
Subject: Re: [SDL] Clean Up Code belongs

so this leads me to my next question which is would it be alright if SDL_Quit( )
was called twice? What if I have SDL_Quit( ) in my destructor and also in my
error catching routine, would that pose a problem…

if yes then is there a way to check if sdl_quit was called? similarily to
SDL_WasInit but dealing with the quit function rather than the init function.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

so the ideally it would look like:

constructor
{
// do not initialize SDL
}

initFunction( )
{
// init all SDL functions
}

~destructor
{
//clean everything up
}

Armond Sarkisian wrote:

so the ideally it would look like:

constructor
{
// do not initialize SDL
}

initFunction( )
{
// init all SDL functions
}

~destructor
{
//clean everything up
}

No, ideally it would look like:

class sdl_system {
public:
sdl_system() { // constructor
// Initialize SDL, and only SDL.
}
~sdl_system() {
// Clean up SDL, and only SDL.
}
};

One class, one responsibility. If SDL fails to initialize, there is no
need to call the destructor. If SDL initializes correctly, the
constructor succeeds and the destructor is called normally when the
object leaves scope.–
Rainer Deyke - rainerd at eldwood.com