Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 28th, 2009, 3:42 AM   #1
magnus
Programmer
 
Join Date: Sep 2008
Posts: 95
Rep Power: 10 magnus is on a distinguished road
Is calling destructor from the constructor a bad practice?

If, say, my constructor opens files and/or allocates dynamic memory, but something in the constructor fails, should I just call the destructor to make sure all the resources are released, or is there a better way of doing it?
magnus is offline   Reply With Quote
Old Nov 1st, 2009, 3:55 AM   #2
hello2
Newbie
 
Join Date: Nov 2009
Posts: 3
Rep Power: 0 hello2 is on a distinguished road
Re: Is calling destructor from the constructor a bad practice?

Jesus... how do you come up with these questions?
hello2 is offline   Reply With Quote
Old Nov 1st, 2009, 5:57 AM   #3
magnus
Programmer
 
Join Date: Sep 2008
Posts: 95
Rep Power: 10 magnus is on a distinguished road
Re: Is calling destructor from the constructor a bad practice?

Huh?
magnus is offline   Reply With Quote
Old Nov 1st, 2009, 6:54 AM   #4
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: Is calling destructor from the constructor a bad practice?

You dp not call the destructor directly. If you need to clean up then put the code in a different class method and call that from the destructor as well as wherever you want. Or the constructor could throw an exception.
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline   Reply With Quote
Old Nov 1st, 2009, 9:48 AM   #5
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 331
Rep Power: 13 Cache is on a distinguished road
Re: Is calling destructor from the constructor a bad practice?

Every individual resource that must be acquired and subsequently freed should have its lifetime handled by RAII (think boost::scoped_ptr). If shared ownership is required then some type of reference count should probably be applied (think boost::shared_ptr/intrusive_ptr).

Learning RAII should be the number one priority for anyone wanting to write leak free, exception safe code in C++.

RAII:
http://en.wikibooks.org/wiki/C++_Programming/RAII

Smart Pointers (read all of this one):
http://onlamp.com/pub/a/onlamp/2006/...rs.html?page=1

Once you've read through those the question in your OP should dissolve.
Cache is offline   Reply With Quote
Old Nov 1st, 2009, 7:18 PM   #6
L7Sqr
Professional Programmer
 
Join Date: Jun 2005
Location: here
Posts: 440
Rep Power: 13 L7Sqr will become famous soon enough
Re: Is calling destructor from the constructor a bad practice?

Quote:
Originally Posted by Cache
Learning RAII should be the number one priority for anyone wanting to write leak free, exception safe code in C++.
There is a world more than just RAII. Consider the following:
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. class Foo {
  4. char * str;
  5. public:
  6. Foo () {
  7. std::cerr << "Foo()" << std::endl;
  8. str = new char[255];
  9. }
  10. ~Foo () {
  11. std::cerr << "~Foo()" << std::endl;
  12. delete[] str;
  13. }
  14. };
  15.  
  16. class error {};
  17.  
  18. int main () {
  19. Foo foo;
  20. throw error();
  21. return 0;
  22. }
The destructor is never called and memory is leaked. Good coding practices are more than an acronym or two. Not that you shouldn't be aware of the wealth of techniques available, but dont be foolish enough to believe that there is ever one 'silver bullet'.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]

My Blog
L7Sqr is offline   Reply With Quote
Old Nov 1st, 2009, 9:02 PM   #7
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 331
Rep Power: 13 Cache is on a distinguished road
Re: Is calling destructor from the constructor a bad practice?

You quote me:
Quote:
Learning RAII should be the number one priority for anyone wanting to write leak free, exception safe code in C++.
I said "number one priority". Not "the only thing you ever need to learn ever".
Besides, is there anyone here who actually disagrees with that statement?

Quote:
The destructor is never called ...
Actually, that's not guaranteed by the standard. Any particular compiler may or may not call the destructor. That behaviour is implementation-defined.
Cache is offline   Reply With Quote
Old Nov 2nd, 2009, 12:09 AM   #8
hello2
Newbie
 
Join Date: Nov 2009
Posts: 3
Rep Power: 0 hello2 is on a distinguished road
Re: Is calling destructor from the constructor a bad practice?

this means you should assume it's not called, if the standard does not guarantee it is called
hello2 is offline   Reply With Quote
Old Nov 2nd, 2009, 3:11 AM   #9
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 331
Rep Power: 13 Cache is on a distinguished road
Re: Is calling destructor from the constructor a bad practice?

Quote:
Originally Posted by hello2 View Post
this means you should assume it's not called, if the standard does not guarantee it is called
Indeed. But let's not confuse convenient assumptions with statements of fact.
Cache is offline   Reply With Quote
Old Nov 2nd, 2009, 11:22 AM   #10
L7Sqr
Professional Programmer
 
Join Date: Jun 2005
Location: here
Posts: 440
Rep Power: 13 L7Sqr will become famous soon enough
Re: Is calling destructor from the constructor a bad practice?

Quote:
Originally Posted by Cache
But let's not confuse convenient assumptions with statements of fact.
Right. RAII guarantees neither leak-free or exception safe code.
The single most important thing in ensuring leak free code is to understand how memory is managed: from the nuts and bolts to the higher order things. This ensures that when a free is not followed by a delete (behind whatever layer that conceals it), or when a function is called with a pointer vs a reference vs a copy, or any of the other nuances that accompany the space the details are understood by the developer.
Same goes for exceptions except that the matter is often times more complex due to the implementation details you elude to.

Again, nothing wrong with the RAII paradigm, but it serves a developer little when the behaviors it helps to overcome are not well understood.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]

My Blog
L7Sqr is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dealing with Bad Project Managers csrocker101 Coder's Corner Lounge 2 May 31st, 2009 4:02 AM
Static Fields And Classes - Best Programming Practice pal C# 2 Jul 25th, 2008 4:38 AM
website for practice problems and contests gogoc Coder's Corner Lounge 13 May 27th, 2008 2:59 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC