Jump to content
  • Advertisement
Sign in to follow this  
Subotron

Let a class instance delete itself (at errors)

This topic is 4553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Er, yes. Brain fart. I was thinking of delete being called, not the destructor being called. Thanks for the correction.

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!