Let a class instance delete itself (at errors)

Started by
17 comments, last by Subotron 18 years, 2 months ago
I was wondering if this is possible (and how): I have a class CL, which I make an instance of: CL* instance = NULL; I now initialize this instance, by calling a second line: instance = new CL(argumentslist); This triggers the constructor, which itself triggers some boolean function which has to return a true value, else some termination function (NOT the destructor, the destructor itself needs more code than just this termination function because if the initialization DOES go right more stuff is done that needs to be undone in the destructor) is called. In this function I free all memory again to prevent leaks. But one thing's missing. If the initialization fails, this doesn't work: if (!instance) return 0; The instance still exists (guess because I didn't call the constructor :)). Is there a way I can destroy the instance totally from within if initialization fails? Thanks a lot in advance!
Advertisement
Throw an exception?

Edit: oh, from within: no not really
Throwing an exception is The Right Thing To Do. Then your destructor will be called, and you can clean up there. If your destructor is not capable of cleaning up from a failed constructor, then you should fix that.
Quote:Original post by Subotron
Is there a way I can destroy the instance totally from within if initialization fails?

You could do "delete this" ... lol, but, that's not always a wise thing to do...

As a simple workaround for your problem, try doing something like this:
CL* instance = new CL(arguments);if(instance->initialize() == false){    delete instance;    instance = null;}else{    // continue on normally...}

Where initialize() is that boolean function you're calling from within the constructor.
Thanks a lot, but could you please tell me how to do this?

edit: omega, that would work, but I really want the boolean function to be called from within the constructor, instead of seperately.
You should wrap your costructor code in try {} and throw execeptions when anythings goes wrong, releasing the resources in the catch() {} block and throwing the same or another exception.
Of course, this requires exception-safe code. [smile]
Quote:
Throwing an exception is The Right Thing To Do. Then your destructor will be called,

However, an exception thrown from a constructor will not invoke the destructor of said object (an object that is not constructed should not be destructed, and an object is not constructed until the successful completion of its constructor). Thus if a constructor has already done something that should be cleaned up when an exception must be thrown, than the cleanup must occur then (this is why the RAII idiom is useful).

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

And to the OP: look up the "resource acquision is initialization" idiom. It is legal to, f.e.x., perform a "delete this" within a member function of an object, but its dangerous and you need to be sure you know what you are doing. It is not the proper solution to your problem.
Side question here... In MSVC 2003, there's an option under Project->Properties->C/C++->Code Generation->Enable C++ Exceptions ... Is this required to be on in order to have exceptions in the code? I haven't done much with exceptions since VC++ 6.
Er, yes. Brain fart. I was thinking of delete being called, not the destructor being called. Thanks for the correction.
ok, guess I have to put in a different question:

suppose at some point in my constructor something fails (the initialization function returns false), can I then call the destructor from within the constructor?

Like this:
class::class(){    if (!initialize)        Call_destructor_here();}

This topic is closed to new replies.

Advertisement