10 Replies - 8415 Views - Last Post: 30 March 2011 - 04:04 PM Rate Topic: -----

#1 JustLearning  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 07-August 09

STL vector of objects - objects are deleted twice?

Posted 29 March 2011 - 01:56 PM

I have a question concerning the STL vector (it is my first try using it). I am really hoping someone can help me with out the need to post my code, but I would completely understand if this is not possible. But, please let me explain my problem...

I have made a templatized class (not sure if that is the correct way to say that). The class contains a static int count that is incremented each time a new object of that class is constructed and is decremented when an object is destroyed (that is when the destructor is used). I have made a vector that uses the .push_back method to create and store objects of the mentioned class in the vector. It seems to me that something very funny is happening when I create the objects this way. To try to figure this out, I have my constructor and destructor print me a message when they are used. From this I see that, as I expected, I am only creating one object (I only see my constructor message one time), but I see the destructor is used 2 times (making it seem like I have two objects even though it only looks like I have one). The major problem this is causing me is that my count is being decremented 2x for each object instead of only once (I assume there are other problems this is causing which I don't understand).

I am hoping someone can please let me know if what I explained is possible, why it is happening and how I can fix this problem.

Oh, in case it matters, the reason I tried using the vector was so it can be dynamic (unlike those arrays which you have decide how big they should be before compile time).

Thanks so much!!!

Is This A Good Question/Topic? 0
  • +

Replies To: STL vector of objects - objects are deleted twice?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4071
  • View blog
  • Posts: 12,559
  • Joined: 25-December 09

Re: STL vector of objects - objects are deleted twice?

Posted 29 March 2011 - 02:04 PM

You will need to post some code that illustrates your problem. You should try to post the smallest compilable program that illustrates your problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: STL vector of objects - objects are deleted twice?

Posted 29 March 2011 - 02:09 PM

I don't know why you are seeing your constructor message only once, but I believe that you are actually creating two objects. You are declaring and instantiating an object when you explicitly call the constructor, and when you push that object onto the vector, the vector is calling your implicit copy constructor and instantiating a second object.

edit: Note that the object in the vector is not the same object that you created. The STL containers create copies of the objects put into them.

This post has been edited by r.stiltskin: 29 March 2011 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 JustLearning  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 07-August 09

Re: STL vector of objects - objects are deleted twice?

Posted 29 March 2011 - 02:35 PM

This is the code I use in main to create my object in the vector:

vector<className<int> > myVector;
myVector.push_back( className<int>(7) );




I am thinking the problem has something to do with creating the copy you mention...is it possible to avoid making that copy? Or is there is some other kind of dynamic array that does not make these copies? When I create an object in main that is not stored in a vector, both the constructor and destructor is called on the expected number of times. And, I have done quite a of bit testing to make sure the methods of this class work as I expect...it is only when I try to store the objects in the vector that I have problems.

Thanks for any more help anyone has!
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: STL vector of objects - objects are deleted twice?

Posted 29 March 2011 - 02:46 PM

What's the value of count immediately after pushing the object onto the vector?
Was This Post Helpful? 0
  • +
  • -

#6 JustLearning  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 07-August 09

Re: STL vector of objects - objects are deleted twice?

Posted 29 March 2011 - 03:00 PM

If I add a line to print the count immediately after the two lines I posted I see that the count is zero. Here is my more detailed analysis for what happens after those three lines (I added a line to the constructor and destructor after the count is incremented or decremented to print the count out for a better idea of what is happening):

(1) the constructor has been called (the count is one)
(2) the destructor has been called (the count is zero)
(3) the count is zero (as determined by the method that gets the count)
(4) the destructor is called again (the count is -1)
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: STL vector of objects - objects are deleted twice?

Posted 29 March 2011 - 06:44 PM

There are two kinds of implicit constructors that the compiler supplies when you don't write them explicitly:
- a default (no-argument) constructor (which is supplied only if you don't write any constructor at all
- a default copy constructor (a copy constructor is a constructor that takes an object of this class as its argument -- it makes a copy of that object) which is supplied if you don't write a copy constructor

You wrote a constructor but you didn't write a copy constructor, so the compiler supplies one -- which does nothing but create a copy of the object sent as its argument. It doesn't print your "constructor has been called" message and it doesn't increment your counter.

When you add your object to the vector with
myVector.push_back( className<int>(7) );
- first your constructor is called (className<int>(7)) to instantiate an object -- printing your message and incrementing count
- then the implicit copy constructor is called, making a copy of that object to add to the vector, but not incrementing count or printing anything
- then the destructor is called to destroy the first object - printing a message and decrementing count
and when you clear the vector, the destructor is called again to destroy the copy, printing the message and decrementing count again

There is no getting around the copying -- that's necessary to add the object to any container, unless you merely create a pointer to the object and put the pointer in an array, vector, etc.

But the correct solution is simply to write your own copy constructor which will increment count when the copy is made.
Was This Post Helpful? 1
  • +
  • -

#8 JustLearning  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 07-August 09

Re: STL vector of objects - objects are deleted twice?

Posted 29 March 2011 - 08:02 PM

Thank you very much!!! I don't know anything about copy constructors, but will try to learn.
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: STL vector of objects - objects are deleted twice?

Posted 29 March 2011 - 08:41 PM

It's easy -- it's just a constructor that takes a reference to an object of the class as its argument. (Yours will also need a template argument.) It assigns the value of each of the data members of the argument into its own corresponding data member. Like other constructors, it has no return type.
Was This Post Helpful? 1
  • +
  • -

#10 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: STL vector of objects - objects are deleted twice?

Posted 30 March 2011 - 03:05 PM

In general, if you ever use either a destructor, a copy constructor or an overloaded assignment operator in your class, then you most likely need all three. (This pearl of wisdom is often referred to as the "rule of 3", although while its not a hard-and-fast rule, it applies in most situations)
Was This Post Helpful? 0
  • +
  • -

#11 JustLearning  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 07-August 09

Re: STL vector of objects - objects are deleted twice?

Posted 30 March 2011 - 04:04 PM

Thanks!!! Everything works as expected now that I have the copy constructor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1