Jump to content
  • Advertisement
Sign in to follow this  
YourGodIsFake

Bad to run code from destructor?

This topic is 4911 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

Is it a bad idea to run non-destructing code from the deconstructor? For instance, the program I am working on at work deals with setting up a database connection and also checks out a product license (like a CD key) every time the program is run. These features are deallocated in the deconstructor. If I wanted to "restart" my program and connect to a different database, I would need to do so after deallocating the product license because otherwise it will produce an error because more than one license will be checked out at one time. So, if I call CreateProcess() to open the program again at the very end of the deconstructor, would that cause any problems? BTW, I am using MSVC++ 6.0.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Is it a bad idea to run non-destructing code from the deconstructor?


It is sometimes a legitimate strategy, when classes get designed for the explicitg purpose of having code run in their constructor or destructor (use the correct terminology!) that is not related to establishing the object's state (it may actually be stateless).

However, injecting unrelated actions into your object's cleanup is a Bad Idea™. Remember that destructors (and deallocation functions) must never fail (must not throw exceptions, etc...)


Quote:
If I wanted to "restart" my program and connect to a different database, I would need to do so after deallocating the product license because otherwise it will produce an error because more than one license will be checked out at one time.


That doesn't mean that the destruction process itself should be the trigger for the reconnection. Its purpose is to release the license, not to try and reacquire it and connect to a different machine...

Quote:
So, if I call CreateProcess() to open the program again at the very end of the deconstructor, would that cause any problems?


I wouldn't do that if I were you. Does destroying the object always mean you're going to start a new process? Isn't that behaviour best handled in the client code:


{
DoStuff();

{
YourObject obj;
....
// Destruction happens here
}

CreateProcess();
}

Share this post


Link to post
Share on other sites
I don't know why I called it the "deconstructor," probably from seeing so many people call it that here. Anyway, I guess I didn't explain it too well. There would be a flag variable that, when the user chooses to restart, would be set. Then in the destructor, check the flag, and if its true, it would call CreateProcess(). Otherwise, it would just finish the destruction code. However, I tried running it and it did not restart at all, and the next time I tried to run it, it froze. I had to terminate the process and then reopen it. So, I guess it's not such a good idea and I have answered my own question. Thank you for the quick help though!

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!