944,043 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6682
  • C++ RSS
Oct 17th, 2005
0

Why is destructor called twice?

Expand Post »
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Oct 17th, 2005
0

Re: Why is destructor called twice?

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 17th, 2005
0

Re: Why is destructor called twice?

Makes sense. To test, I made it a vector of Test * and only got one destruction call:


C++ Syntax (Toggle Plain Text)
  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....
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Oct 17th, 2005
0

Re: Why is destructor called twice?

[edit](Never mind)
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Oct 17th, 2005
0

Re: Why is destructor called twice?

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Concordance, Linked Lists and classes
Next Thread in C++ Forum Timeline: Anyway can help me to solve this problem???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC