Thread: How does delete work?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    How does delete work?

    Hello everyone,


    Just interested to learn how C++ implements delete this statement internally. From logical point of view, I can not imagine how to implement a component which destroy itself.

    What makes me confused is the component is relying on itself even in the process of destroy, how could it destroys itself from itself?

    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    delete de-allocates the memory held by the data after it has called the destructors for the actual object. So it's all fine to do that - the object is still there until the destructor has been finished, and then disappears.

    Of course, you need to know that "this" points to memory allocated by new - otherwise you'll crash and burn.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I believe it works something like this:
    Code:
    delete pObj;
    
    // becomes this:
    try
    {
       pObj->~Object();  // Call the destructor.
    }
    catch (...)
    {
       std::terminate();  // Instantly kill the program if an exception is thrown.
    }
    
    free( pObj );  // Deallocate memory.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    #include <iostream>
    
    struct A
    {
        ~A() {throw 1;}
    };
    
    int main()
    {
        try {
            A* p = new A;
            delete p;
        }
        catch (int n) {
            std::cout << n << '\n';
        }
    }
    Well, this doesn't terminate. It is however wrong to throw exceptions from the destructor because two exceptions must not be propagating at the same time (the destruction could be called because of another exception in the first place).

    Concerning delete this;. Given the restrictions, I'm not so sure where it would be useful and why I wouldn't rather delete the object from the calling code. But you never know...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I've used delete this for deleting objects from a Factory class in a separate DLL. Something like this:
    Code:
    class CSomething
    {
    public:
       void Delete() {delete this;}
    ...
    };
    Calling code would then delete the pointer safely (across module boundaries) by calling the Delete() member function for their object pointer.
    Code:
    CSomething* obj = factory.CreateSomething();
    ...
    obj->Delete();
    obj = NULL;

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    delete this; is required in implementing reference counted objects.

    Since it doesn't delete the code that is executing out from under the processor, but only the data that it was once working on (and isn't now) then everything is fine.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  2. can we debug into delete
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2007, 05:45 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM