0

In a situation where we have a derived class object binded to the base class pointer bPtr, and a derived class pointer dPtr pointing to the same object via dynamic_cast<D*>, what is the proper way to cleanup the resources?

class B {
public:
    B() { cout << "B_ctor" << endl; }
    virtual ~B() { cout << "B_destructor" << endl; }
    virtual void foo() { cout << "B_foo()" << endl; }
};

class D : public B {
public:
    D() { cout << "D_ctor" << endl; }
    ~D() { cout << "D_destructor" << endl; }
    void foo() { cout << "D_foo()" << endl; }
};   

int main() 
{ 
    B * bPtr = new D;
    D * dPtr = dynamic_cast<D*>(bPtr);
    bPtr->foo();
    dPtr->foo();
    //delete bPtr;
    delete dPtr;
}

I chose to delete dPtr. I can also choose bPtr. Whenever I do both VS2015 compiler flashes an error. So my question is what is the standard way to deal with given situation?

Also, whenever I change my code to

D d;
B * bPtr = &d;
D * dPtr = dynamic_cast<D*>(bPtr);

I can't delete neither of pointers. The program just aborts. Why?

Damian
  • 4,395
  • 4
  • 39
  • 67
user2376997
  • 501
  • 6
  • 22
  • 2
    You should not delete either pointer. It's Undefined Behavior to do so. Only delete memory you allocated with `new`. `bPtr` and `dPtr` point to an object in the automatic storage. If you don't know what that is here: https://stackoverflow.com/questions/9181782/why-are-the-terms-automatic-and-dynamic-preferred-over-the-terms-stack-and – drescherjm Mar 06 '19 at 03:47
  • You need to be more specific than "flashes an error". – Mark Ransom Mar 06 '19 at 04:02

2 Answers2

4

D d defines an Automatically allocated variable. It is automatically disposed of when it goes out of scope. You do not need to delete it and since it was not allocated with new you cannot delete it.

Only delete what you new and delete[] what you new []. If you didn't do either don't delete it. If you didn't allocate a variable, check the documentation for the function that provided the variable to see how to properly dispose of it.

Additional helpful reading: Why are the terms "automatic" and "dynamic" preferred over the terms "stack" and "heap" in C++ memory management?

user4581301
  • 33,082
  • 7
  • 33
  • 54
4

You have two pointers to a single object. If the object was created with new then you must destroy it with delete. If it was not, as in your second example, then it's an error to use delete.

Since there was only one new you should have exactly one delete to destroy it. It does not matter which pointer you use, since they are copies of each other and the destructor is declared virtual. If the destructor was not virtual, you would need to delete the exact pointer type of the object that was created with new.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622