Topic: Can a class delete itself?


Author: "John M. Vreeland \(Vreejack\)" <jmvree@infi.net>
Date: 1998/08/26
Raw View
I such a class does not make sure that it is instantiated on the heap then it
is doomed; however, proper use of placement new can determine if such objects
are placed on the stack or heap.  Scott Meyers gave an excellent example of
such a class in his sequel, "31 More Ways to Write Better C++"   (Or something
like that)

This has probably  saved me from all sorts of hard-to-detect bugs.  In the
event that I (mistakenly, I presume) place one of these objects on the stack
and then call the destructor,  a call to the inherited function
HeapItem::IsOnHeap() can be used to determine whether-or-not to delete the
object.

Tom McKearney wrote:

> The line:
>
> delete this;
>
> is used in most Modeless dialogs in MFC on the OnPostNcDestroy() function.
>
> It definitely works there...
>
> Niki Estner wrote in message <6r7ita$geo$1@salyko.cube.net>...
>
> >>can I use "delete this;" in a non-static member function in standard C?
> >No, you can't new and delete are C++ keywords - you cannot use them in
> >standard C :-)
> >>WIth my implementation, I bet it will work...but I want to know what the
> >>standard says.
> >You're right, it works, I've done it, too.
> >I don't think the standard actually covers this, but if you write a compiler
> >according to the standard, it will work. In plain English: If the
> >compiler-developers haven't thought about it, it will work, if they have,
> >they might have forbidden it.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Niki Estner" <niki.estner@cube.net>
Date: 1998/08/16
Raw View
Anthony DeRobertis wrote...
>Although I know this would be a VERY bad design, assuming:
This is definitely right, and I'm sure it's defined in some standard
>
>   1) After this line of code, no class member will be used again;
>   2) After this line of code, no pointer to the class will be used; and
>   3) The class was created using 'new',
>
>can I use "delete this;" in a non-static member function in standard C?
No, you can't new and delete are C++ keywords - you cannot use them in
standard C :-)
>WIth my implementation, I bet it will work...but I want to know what the
>standard says.
You're right, it works, I've done it, too.
I don't think the standard actually covers this, but if you write a compiler
according to the standard, it will work. In plain English: If the
compiler-developers haven't thought about it, it will work, if they have,
they might have forbidden it.
>
>Don't ask why...
Bet I won't.
>
    Niki Estner
    niki.estner@cube.net




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Tom McKearney" <me@nospam.com>
Date: 1998/08/21
Raw View
The line:

delete this;

is used in most Modeless dialogs in MFC on the OnPostNcDestroy() function.

It definitely works there...

Niki Estner wrote in message <6r7ita$geo$1@salyko.cube.net>...

>>can I use "delete this;" in a non-static member function in standard C?
>No, you can't new and delete are C++ keywords - you cannot use them in
>standard C :-)
>>WIth my implementation, I bet it will work...but I want to know what the
>>standard says.
>You're right, it works, I've done it, too.
>I don't think the standard actually covers this, but if you write a
compiler
>according to the standard, it will work. In plain English: If the
>compiler-developers haven't thought about it, it will work, if they have,
>they might have forbidden it.



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David R Tribble <david.tribble@noSPAM.central.beasys.com>
Date: 1998/08/21
Raw View
Niki Estner wrote in message <6r7ita$geo$1@salyko.cube.net>...
>>> can I use "delete this;" in a non-static member function in standard
>>> C?

>> No, you can't new and delete are C++ keywords - you cannot use them
>> in standard C :-)

>>> WIth my implementation, I bet it will work...but I want to know what
>>> the standard says.

>> You're right, it works, I've done it, too.
>> I don't think the standard actually covers this, but if you write a
>> compiler according to the standard, it will work. In plain English:
>> If the compiler-developers haven't thought about it, it will work, if
>> they have, they might have forbidden it.

Tom McKearney wrote:
> The line:
>
> delete this;
>
> is used in most Modeless dialogs in MFC on the OnPostNcDestroy()
> function. It definitely works there...

'delete this' is perfectly legal, but it does entail some danger.
It's probably a bad idea most of the time, but sometimes it's
appropriate.

Here's a toy example showing how 'delete this' can be used:

    class Item      // Keeps a stack of items
    {
    public:
                    Item(): m_prev(NULL) { ... }
        Item *      push(Item *prev);
        Item *      pop();
        ...
    private:
        Item *      m_prev;
        ...
    };

    Item * Item::push(Item *prev)   // Push an item on the stack
    {
        m_prev = prev;
        return this;
    }

    Item * Item::pop()              // Pop an item from the stack
    {
        Item *   prev;

        prev = m_prev;
        m_prev = NULL;
        delete this;         // Does a 'delete this'!
        // At this point, we cannot access any member of *this,
        // because there is no *this object
        return prev;
    }

    void myFunc()
    {
        Item *    root = NULL;

        for (int i = 1;  i <= N;  i++)
        {
            Item *    it;

            it = new Item;
            root = it->push(root);
        }
        // root now points to a stack of N items

        while (root != NULL)
        {
            frob(root);
            root = root->pop();    // Does a 'delete this'!
        }
        // root is now NULL and the stack is empty
    }

Note that 'root->pop()' deletes '*root' and returns a pointer to
the next item on the stack (which was previously pushed).  If you
don't reassign the 'root' pointer at this point, you're asking for
trouble.

(I realize that there may be better ways of doing this, but this
serves simply as a small example of using 'delete this'.)

-- David R. Tribble, dtribble@technologist.com --


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: derobert@erols.com (Anthony DeRobertis)
Date: 1998/08/07
Raw View
Although I know this would be a VERY bad design, assuming:

   1) After this line of code, no class member will be used again;
   2) After this line of code, no pointer to the class will be used; and
   3) The class was created using 'new',

can I use "delete this;" in a non-static member function in standard C?
WIth my implementation, I bet it will work...but I want to know what the
standard says.

Don't ask why...

--
Windows 95 (win-DOH-z), n. A thirty-two bit extension and graphical
shell to a sixteen bit patch to an eight bit operating system
originally coded for a four bit microprocessor which was used in a PC
built by a two bit company that couldn't stand one bit of competition.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: AllanW@my-dejanews.com
Date: 1998/08/07
Raw View
Yes.

An example where this is merely bad design (as opposed to "VERY bad
design") is COM components in 32-bit Windows. Here, the object is
constructed by an "Object Factory" -- and after that, it receives a
variety of messages. One message is "Increase the reference count"
which means that the address of this object has been given out
publicly. Another message is "Decrease the reference count" which
means that one of the external users has finished using this object
and will not send any more messages. The object is responsible for
keeping a "reference count," and when the count goes to 0, the object
is responsible for deleting itself.

The crucial thing about this design is that the originator of the
messages is not part of the C++ program -- it could be some other
language, some other process, possibly even some other computer.
The only clue that the program will ever receive to indicate that
the object is no longer needed, is the final "Remove Reference"
message which brings the reference count to 0.

When a class doesn't fall into that strange category (responding
to external messages not controlled by your program), it shouldn't
need to do anything so hokey.

> Don't ask why...

But I do ask why. Other than the case cited above, this is usually
a poor design.

> --
> Windows 95 (win-DOH-z), n. A thirty-two bit extension and graphical
> shell to a sixteen bit patch to an eight bit operating system
> originally coded for a four bit microprocessor which was used in a PC
> built by a two bit company that couldn't stand one bit of competition.

You might want to change your sig. Some would consider it a slam,
and that's not appropriate in this newsgroup.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Edward Diener <eddielee@abraxis.com>
Date: 1998/08/08
Raw View
Anthony DeRobertis wrote:

> Although I know this would be a VERY bad design, assuming:
>
>    1) After this line of code, no class member will be used again;
>    2) After this line of code, no pointer to the class will be used; and
>    3) The class was created using 'new',
>
> can I use "delete this;" in a non-static member function in standard C?
> WIth my implementation, I bet it will work...but I want to know what the
> standard says.
>
> Don't ask why...

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]