"delete this" for object in Stack

Discussion in 'C++' started by thomas, Oct 15, 2010.

  1. thomas

    thomas Guest

    Hi,

    ------------
    class A {
    public:
    ~A(){}
    void Release(){ delete this; }
    };
    -----------
    A *a = new A();
    a->Release(); --> Method I
    A a2;
    a2.Release(); --> Method II
    -----------

    a is created in Heap while a2 is in Stack.
    When I call "Release()", thus "delete this;" to these two different
    kinds of object, what will happen?
    If "delete this" just involves calling the destructor, I suppose they
    are equivalent.
    Any suggestions? Thanks.

    tom
     
    thomas, Oct 15, 2010
    #1
    1. Advertising

  2. On Oct 15, 9:22 am, thomas <> wrote:
    > Hi,
    >
    > ------------
    > class A {
    > public:
    >       ~A(){}
    >        void Release(){ delete this; }};
    >
    > -----------
    > A *a = new A();
    > a->Release();       -->    Method I
    > A a2;
    > a2.Release();      -->    Method II
    > -----------
    >
    > a is created in Heap while a2 is in Stack.
    > When I call "Release()", thus "delete this;" to these two different
    > kinds of object, what will happen?
    > If "delete this" just involves calling the destructor, I suppose they
    > are equivalent.
    > Any suggestions? Thanks.
    >
    > tom


    Calling delete on an object for which memory has not been allocated
    explicitly using *new* invokes Undefined Behaviour.

    Undefined Behaviour means *anything* can happen. Don't write such code.
     
    Prasoon Saurav, Oct 15, 2010
    #2
    1. Advertising

  3. On Oct 15, 9:22 am, thomas <> wrote:
    > Hi,
    >
    > ------------
    > class A {
    > public:
    >       ~A(){}
    >        void Release(){ delete this; }};
    >
    > -----------
    > A *a = new A();
    > a->Release();       -->    Method I
    > A a2;
    > a2.Release();      -->    Method II
    > -----------
    >
    > a is created in Heap while a2 is in Stack.
    > When I call "Release()", thus "delete this;" to these two different
    > kinds of object, what will happen?
    > If "delete this" just involves calling the destructor, I suppose they
    > are equivalent.
    > Any suggestions? Thanks.
    >
    > tom


    Calling delete on something,memory for which, has not been allocated
    explicitly using *new* invokes Undefined Behaviour.
    Undefined Behaviour means *anything* can happen. Don't write such
    code.
     
    Prasoon Saurav, Oct 15, 2010
    #3
  4. thomas

    werasm Guest

    On Oct 15, 6:22 am, thomas <> wrote:
    > Hi,
    >
    > ------------
    > class A {
    > public:
    >       ~A(){}
    >        void Release(){ delete this; }};
    >
    > -----------
    > A *a = new A();
    > a->Release();       -->    Method I
    > A a2;
    > a2.Release();      -->    Method II
    > -----------
    >
    > a is created in Heap while a2 is in Stack.
    > When I call "Release()", thus "delete this;" to these two different
    > kinds of object, what will happen?
    > If "delete this" just involves calling the destructor, I suppose they
    > are equivalent.
    > Any suggestions? Thanks.
    >
    > tom


    Hi,

    Make sure you read this FAQ:

    http://www.parashift.com/c -faq-lite/freestore-mgmt.html#faq-16.15

    You cannot call delete for objects allocated on the stack (amongst
    others - see FAQ). This means that you have to make sure your
    objects are heap allocated. You could do this by making the
    constructor private and having a static function that returns
    a heap instance.

    class A
    {
    public:
    void Release(){ delete this; }
    static std::auto_ptr<A> makeA()
    {
    return std::auto_ptr<A>( new A );
    }
    private:
    A(){ }
    };

    I just use an auto pointer here because it makes
    it clear that the caller takes ownership of the
    pointer (assuming he calls the function that eventually
    performs deallocation.

    Kind regards,

    Werner
     
    werasm, Oct 15, 2010
    #4
  5. thomas

    Goran Guest

    On Oct 15, 6:22 am, thomas <> wrote:
    > Hi,
    >
    > ------------
    > class A {
    > public:
    >       ~A(){}
    >        void Release(){ delete this; }};
    >
    > -----------
    > A *a = new A();
    > a->Release();       -->    Method I
    > A a2;
    > a2.Release();      -->    Method II
    > -----------
    >
    > a is created in Heap while a2 is in Stack.
    > When I call "Release()", thus "delete this;" to these two different
    > kinds of object, what will happen?
    > If "delete this" just involves calling the destructor, I suppose they
    > are equivalent.


    Not at all. operator delete invokes the destrucctor and then frees
    allocated memory. Just like operator new allocates memory, then calls
    the constructor. That's a rather fundamental aspect of what they do.

    > Any suggestions? Thanks.


    Others already explained.

    I'll add this: "delete this" is in general a bloody stupid idea with
    rare situations where it's expedient. Your snippet is just a massive
    bug.

    C++ language, however, does allow you to design a type so that clients
    have to use it on the heap, and that you have to call e.g. a Release
    function (name is incidental). E.g.

    class heap_only_please
    { // intentionally private:
    heap_only_please() {...}
    heap_only_please(const heap_only_please& ) {...};
    ~heap_only_please() {...}
    heap_only_please(params) {...}
    public:
    static heap_only_please* create(params)
    {
    return new heap_only_please(params);
    }
    void Release() { delete this; }
    // Want refcounting? Put it in Release! ;-)
    };

    Goran.
     
    Goran, Oct 15, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Surinder Singh
    Replies:
    1
    Views:
    1,229
    Richard Bos
    Dec 20, 2007
  2. Casey Hawthorne
    Replies:
    3
    Views:
    1,116
    Flash Gordon
    Nov 1, 2009
  3. Debajit Adhikary
    Replies:
    36
    Views:
    2,355
    Andre Kaufmann
    Feb 10, 2011
  4. Sam Roberts
    Replies:
    1
    Views:
    232
    Yukihiro Matsumoto
    Feb 11, 2005
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    276
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page