jeudi 1 décembre 2022

Destructor called twice when created as base type - c++ [duplicate]

Having a simple class hierarchy, I instantiate from derived and store it as a base class variable. At the end of the scope, destructor of base is called twice. Making base destructor virtual does not work. It is unwanted in many cases. What is the reason of this bizarre behaviour and what is the correct way of doing this?

Here is an example erroneous case:

#include <iostream>

class Base{
private:
    int* a;

public:
    Base() {
        std::cout << "Base constructor" << std::endl;
        a = new int{6};
    }

    virtual ~Base(){
        std::cout << "Base destructor" << std::endl;
        delete a;
    }
};

class Derived: public Base {
private:
    int* b;

public:
    Derived() {
        std::cout << "Derived constructor" << std::endl;
        b = new int{7};
    }

    ~Derived(){
        std::cout << "Derived destructor" << std::endl;
        delete b;
    }
};

int main() {
//    Base b = Base();  // ok
//    Derived b = Derived();  // ok

    Base b = Derived();  // double free

//    Base b;  // even worse 
//    b = Derived();

//    Base *b;  // ok, but not desired
//    b = new Derived();
//    delete b;

    return 0;
}

I use g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 compiler. Both -std=c++20 and -std=c++11 are same for this example.

Aucun commentaire:

Enregistrer un commentaire