Let a class instance delete itself (at errors)

Started by
17 comments, last by Subotron 18 years, 1 month ago
Quote:Original post by Subotron
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();}


Will the compiler allow you to do this? Yes.

Should you do this? God no.

Since a constructor cannot return a value to indicate failure, you do not allow the classes clients to know there was a failure, so they will use an invalid object as if it was valid. Better to cleanup and throw an exception.

It is rare that you need to call a destructor explicitly. It is even rarer for such code not to have an alternate that is simpler. (eg, moving common cleanup to another function, and adding a member variable to indicate whether the class is initialised properly...

Edit: also remember that if you call a destructor explicitly, the destuctor will be called again when delete is used, or when a stack object goes out of scope. Your destructor must be capable of being called twice.
Advertisement
Just to expand on what I was (incorrectly) talking about before: If you throw an exception from the constructor, the destructor won't be called, but the memory WILL be freed. So even in that case, resources are not leaked.
It's possible to manually invoke a destructor. But you shouldn't do it, especially not in your specific situation. There is a reason that the destructor is not automatically called when a constructor fails (and throws) -- don't try to circumvent this!

Your object has not been fully constructed; therefore it is possible that the destructor could fail, or do something else naughty like clean up a resource that hasn't been allocated yet. Double deletes are preventable but not all kinds of release are. You are just displacing your problem, and in fact making it harder for yourself (you can't throw an exception from a destructor or the runtime will shoot your application dead).

Employ the RAII idiom in your constructors. Be careful that your write exception-safe constructors: ensure that any exit path from the constructor correctly deallocates all resources that need to be.

Yes, its a pain. Yes, its nontrivial. Welcome to the field of programming; its hard. You can always switch to a managed language like C# that helps you with some of this (not that C# is a magic bullet, but there you go).
arggh :)

ok, so I fixed it like this: (please tell me if it's really a good fix)
class::class(arguments){    if (!start(arguments)    {        destroy(); // Safely releases all pointers        throw("NOOOO!");        return;    }}

I now initialize the class like this:
try{    instance = new class(arguments);}catch (char* error){    return 0; // So far only one possible error, so no need to check what happened}
Well, I would throw an exception class, as they make sense to throw, but other than that, yeah.
Indeed. Throw std::exception, or one of its subclasses, or a subclass of your own creation. The classes have constructors that take message parameters, so you can do something like:

try{  throw std::runtime_exception("something bad happened");}catch(std::exception &e){  // Note - caught the base class, allows us to catch the base  // and all derived classes. Also, we caught by reference.   // This is generally good practice.  // Print the error to stderr.  std::cerr << e.what();}
If you're not comfortable with exceptions you could try this:
Class::Class(bool& Success){  Success = false;  if(some_error) return;  if(some_other_error) return;  Success = true;}

Then:
bool Success;Class* C = new Class(Success)if(!Success){  delete C;  return some_error_code;}do_something_else;delete C;
----------------------------------------MagosX.com
Not only is that pretty ugly, but it won't work for your copy constructor.
I think I get exceptions now. I had only seen them once before, recently, so I didn't really know how to use them with my problem. Thanks for the help with that exception class, I didn't know you could do it like that as well.

Guess I'm fixed now. All I need to do now is write a function that takes another function as argument and exception-handles it :)

thanks a lot for all the helpful replies!

This topic is closed to new replies.

Advertisement