7

I'm wonder to know is it possible to delete an object through destructor method?

Constructor and destructor of my class:

class cal
{
    public:
        cal()
        {
            days = 0;
            day = 1;
            month = 1;
            year = 1300;
            leap = true;
        };
        ~cal()
        {
            delete this;
        }
}*calendar = new cal;

How can I delete this pointer through class?

P.S

I forgot to write the following code

cal *calandar = new cal[];

I want to use it in heap not stack

I want to use this class (objects) frequently (many of that object) imagine how many time should I write delete and it make hard understanding, troubleshooting and tracing codes I want them to been destroyed automatically (in heap)

I used following code in my class when I exec "delete[] calendar" it reduce my rams occupied (amount of ram that is used) does it work properly (destroy all objects) by exiting program? Because I use GNU/Linus and it destructs all objects with or without that lines I'm concern about leaking in windows

void DisposeObject() { delete this; }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    why do you need to? why not let calling code delete your object? – mathematician1975 Jul 12 '12 at 16:17
  • If you want to save the user trouble, don't bother! If they're interested in that, they should be using a smart pointer. – chris Jul 12 '12 at 16:19
  • maybe but it is not *compulsory* to use them. And somehwere along the line the word `delete` will be used – mathematician1975 Jul 12 '12 at 16:22
  • 1
    @chris There's nothing wrong with deleting `this`; it's a common idiom. But you don't want to do it in the destructor. – James Kanze Jul 12 '12 at 16:30
  • @JamesKanze, Is it used anywhere other than a `Release()` method? I've never seen it used otherwise. – chris Jul 12 '12 at 16:36
  • @chris In OO programing, it's frequent. An object registers for a number of events concerning whatever it represents, and when an event comes that says what it represents is no longer valid, it deletes itself. (Transaction management can mean that it registers for deletion at the end of the transaction---it's rather difficult to roll back a deleted object. But that's not always the case.) – James Kanze Jul 12 '12 at 18:03

3 Answers3

14

No. By the time the destructor is called, the object is already being destroyed, so delete this is not valid.

The behaviour is undefined, but most likely it will call the destructor recursively, leading to a stack overflow.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Delete this; has defined results, as long as you assure the object was allocated dynamically, and (of course) never attempt to use the object after it's destroyed. http://stackoverflow.com/questions/3150942/c-delete-this – Sergey K. Jul 12 '12 at 18:15
  • @SergeyK.: But `delete this` is not valid in the destructor, since it's already being destroyed. – Mike Seymour Jul 12 '12 at 20:27
  • Sure, but this is not the general case. – Sergey K. Jul 12 '12 at 20:34
  • 2
    @SergeyK.: No, I'm just answering the question that was asked: "is it possible to delete an object through destructor", with an example showing a destructor containing `delete this`. The general case is irrelevant to that question. – Mike Seymour Jul 12 '12 at 20:36
  • The question is - "how can i delete this pointer through class" – Sergey K. Jul 12 '12 at 20:38
11

You can write the code like this:

class cal
{
    public:
        cal()
        {
        };
        ~cal()
        {
        }
        void DisposeObject()
        {
           delete this;
        }
}

And it will call your destructor.

You should not call delete this from your destructor since it will call the destructor recursively, possibly leading to a stack overflow. Anyway, the code you wrote suffers from undefined behavior.

Refer to this question for in-depth discussion: Is delete this allowed?

Community
  • 1
  • 1
Sergey K.
  • 24,894
  • 13
  • 106
  • 174
3

I`m wonder to know is it possible to delete an object through destructor method

Even supposing this was valid - The problem is that nothing would ever call the destructor, so this would never have an effect. The destructor is called when the calling code deletes the object, which in turn can delete any internal objects as needed.

In your case, it doesn't look like you need to do anything in your destructor, however, as you don't have any resources being allocated that need to be explicitly deleted (at least in what you're showing).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373