Enigma22134
Enigma22134

Reputation: 530

Destructor called twice

EDIT: Turns out that this happens whether or not I use a vector. It has to do with objects local to the function having automatic destruction, despite my attempted explicit call to the destructor.

I'm trying to figure out why my object is apparently deleted twice. I was experimenting with the std::vector class and wanted to see how deletion of a object was handled. Can anyone enlighten me as to what is going on in this situation?

Code:

#include<iostream>
#include<vector>
#include"DummyClass.h"
using namespace std;

void main(void){
    //Make vector
    vector<DummyClass> objVect1;

    //Make objects to contain
    DummyClass test;

    //pass by value
    objVect1.push_back(test);   //makes a pass-by-value copy, I think?


    //Delete the objects stored in the array 
    objVect1.clear();   // call dtor (vector's copy)

    test.~DummyClass(); //call dtor on test
}                       //dtor called on test again?

console output:

0033F9DB was constructed
0062C200 was destructed
0033F9DB was destructed
0033F9DB was destructed

The last object is destructed two times. I am trying to figure out what is going on. It seems there was no constructor called for the copy-by-value argument passed into the vector. Can anyone help me figure this out? Thanks!

The class header is:

#pragma once
#include<iostream>
class DummyClass
{
public:
    DummyClass();
    ~DummyClass();
};

The class cpp is:

DummyClass::DummyClass()
{    
    std::cout << this << " was constructed" << std::endl;
}

DummyClass::~DummyClass()
{
    std::cout << this << " was destructed"<< std::endl;
}

Upvotes: 3

Views: 2727

Answers (5)

notdodo
notdodo

Reputation: 149

I cannot comment but I think that the first call is made after clear and the test object is destoyed; the second one is called by your call.

Upvotes: 0

James Adkison
James Adkison

Reputation: 9602

DummyClass test;

This object is in the main function and will go out of scope when main returns. (1 destructor called)

objVect1.push_back(test);

A copy of the DummyClass is added to the std::vector.

objVect1.clear();

The copy is destroyed when clear is called. (1 destructor called)

test.~DummyClass()

You should not be explicitly calling this destructor, it happens automatically when the object goes out of scope (i.e., when the main function returns). (1 destructor called)

The destructor function was called 3 times, as shown above, for 2 object instances. The explicit destructor call should be removed.

Upvotes: 3

Sam Varshavchik
Sam Varshavchik

Reputation: 118445

push_back() copies test into the vector. The destructor is called for the first time when the vector gets clear()ed, and the copy in the vector gets destroyed.

The destructor gets called the second time when you explicitly invoke it.

And the third time when main() returns, and test gets really destroyed.

Upvotes: 0

Alex Zywicki
Alex Zywicki

Reputation: 2313

You don't need to call the destructor at the end of your main. It is automatically called once "test" goes out of scope. Remove the last line of your main.

Upvotes: 0

user2357112
user2357112

Reputation: 281548

The last object is deconstructed two times.

Once when you explicitly destruct it, and then once again when the function ends and all variables local to the function auto-destruct. This is undefined behavior.

You should almost never call a destructor yourself.

Upvotes: 3

Related Questions