Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explicit call to destructor is not destroying my object why?

Tags:

c++

destructor

I'm calling the destructor to deallocate memory but it is not deleting my object. What is the reason behind it?

my code is like this:

class A
{
public: 
    int a;
    A()
    {
        cout << "a" << endl;
    }
};

class B :public A
{
public: 
    int b;
    B()
    {
        cout << "b" << endl; a = 10; b = 20;
    }
    ~B()
    {
        cout << a << b << endl;
    }
};

and I am using it like:

int main()
{
    {
        B b;
        b.~B();
        b.b=100;  // why this step is executed?
    }
    int x;
    cin>>x;
    return 0;
}
like image 562
teacher Avatar asked Sep 13 '11 02:09

teacher


People also ask

Can you call destructor explicitly?

No. You never need to explicitly call a destructor (except with placement new ). A class's destructor (whether or not you explicitly define one) automagically invokes the destructors for member objects. They are destroyed in the reverse order they appear within the declaration for the class.

Does destructor destroy the object?

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

Can you explicitly call a destructor in C++?

Yes, it is possible to call special member functions explicitly by the programmer.

Why is my destructor not called?

There are two reasons that your destructors aren't being called, one is as kishor8dm pointed out that you are using the operator "new" and because of that the "delete" command must be called explicitly.


2 Answers

i m calling destructor to deallocate memory

Why? At language level destructor does not deallocate memory occupied by the object itself.

A non-trivial destructor ends object's lifetime, but it doesn't end the object's storage duration. This means that memory remains allocated, it just becomes "raw" (uninitialized). So, in that sense it is destroying your object.

Meanwhile, a trivial destructor has no effect at all. Even if you call it explicitly, the object's lifetime does not end.

In your case the destructor B::~B is non-trivial though, which formally means that by calling it you ended your object's lifetime. You destroyed it as much a local object can be destroyed. But the memory remains. Attempting to access that memory as a B object simply leads to undefined behavior.

In fact, there's no way to manually deallocate memory occupied by a local object. Local memory is always deallocated automatically.

like image 58
AnT Avatar answered Sep 25 '22 07:09

AnT


You do not call a destructor like that (well, you can but it's generally not done).

For automatic variables like your b, the destructor will be called at some point when the variable goes out of scope. You don't ever need to call the destructor explicitly.

For objects allocated on the heap with new, the destructor will be called after you delete them. In this case, you also don't call the destructor explicitly.

C++03 states in 12.4 Destructors:

Destructors are invoked implicitly:

  • for a constructed object with static storage duration (3.7.1) at program termination;
  • for a constructed object with automatic storage duration (3.7.2) when the block in which the object is created exits;
  • for a constructed temporary object when the lifetime of the temporary object ends;
  • for a constructed object allocated by a new-expression, through use of a delete-expression;
  • in several situations due to the handling of exceptions.

Destructors can also be invoked explicitly.

Note: explicit calls of destructors are rarely needed. One use of such calls is for objects placed at specific addresses using a new-expression with the placement option. Such use of explicit placement and destruction of objects can be necessary to cope with dedicated hardware resources and for writing memory management facilities.

You especially don't do what you're trying to do since the destructor will be called twice, once explicitly by you and once implicitly when b goes out of scope. From that same section of the standard:

Once a destructor is invoked for an object, the object no longer exists; the behavior is undefined if the destructor is invoked for an object whose lifetime has ended. Example: if the destructor for an automatic object is explicitly invoked, and the block is subsequently left in a manner that would ordinarily invoke implicit destruction of the object, the behavior is undefined.

This text remains unchanged in the latest draft of C++11 that I have (n3225, November 2010) and it's unlikely it would have changed in essence between that and approval in August 2011.

like image 30
paxdiablo Avatar answered Sep 24 '22 07:09

paxdiablo