Why is destructor called twice?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Why is destructor called twice?

 
0
  #1
Oct 17th, 2005
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4. class Test
  5. {
  6. public:
  7. Test()
  8. {
  9. cout<<"In TEST constructor"<<endl;
  10. }
  11. ~Test()
  12. {
  13. cout<<"In TEST destructor"<<endl;
  14. }
  15.  
  16. };
  17. int main()
  18. {
  19. Test t1;
  20. vector<Test> vec;
  21. vec.push_back( t1 );
  22. cout<<"About to clear vector"<<endl;
  23. vec.clear();
  24.  
  25.  
  26. }

I suspect I'm missing something fundamental. When I run this, I get the cout for the destructor twice. (Why would clearing the vector cause the destructor of Test to be called?)

./Temp
In TEST constructor
About to clear vector
In TEST destructor
In TEST destructor
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 255
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why is destructor called twice?

 
0
  #2
Oct 17th, 2005
A guess:
#include <vector>
#include <iostream>
using namespace std;
class Test
{
		public:
		Test()
		{
				cout<<"In TEST constructor"<<endl;
		}
		~Test()
		{
				cout<<"In TEST destructor"<<endl;
		}

};
int main()
{
		Test t1;
		vector<Test> vec;
		vec.push_back( t1 );
		cout<<"About to clear vector"<<endl;
		vec.clear();
      cout << "done\n";
}

/* my output
In TEST constructor
About to clear vector
In TEST destructor
done
In TEST destructor
*/
The constructor code is the construction of t1. Then a copy constructor is used when it is pushed back on the vector. When the clear() is called, it calls the destructor for the object in the vector. Then t1's destructor is called when it goes out of scope.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Why is destructor called twice?

 
0
  #3
Oct 17th, 2005
Makes sense. To test, I made it a vector of Test * and only got one destruction call:


  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4. class Test
  5. {
  6. public:
  7. Test()
  8. {
  9. cout<<"In TEST constructor"<<endl;
  10. }
  11. ~Test()
  12. {
  13. cout<<"In TEST destructor"<<endl;
  14. }
  15. };
  16. int main()
  17. {
  18. Test t1;
  19. vector<Test *> vec;
  20. vec.push_back( &t1 );
  21. cout<<"About to clear vector"<<endl;
  22. vec.clear();
  23. cout<<"done"<<endl;
  24.  
  25. }
./Temp
In TEST constructor
About to clear vector
done
In TEST destructor

----------------
I would imagine then that if my classes are large that I would be much better served (performance wise) to be holding pointers to my objects than copies of my objects....
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Why is destructor called twice?

 
0
  #4
Oct 17th, 2005
[edit](Never mind)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 255
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why is destructor called twice?

 
0
  #5
Oct 17th, 2005
I can't help but wonder about your intent with pushing an address. Let's say you were reading records from a file into a temporary line of text. You could avoid the constructor and pass only a pointer to the temporary text, but you wouldn't be keeping unique copies of the data that you'd read. In the end you'd have a pile of pointers all pointing to something that is no longer there.

Be careful with what you are doing, and try to avoid premature optimization. Perhaps you could provide a little more of the 'big picture'; but in the end be careful.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4123 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC