Re: delete this; return ret;

Discussion in 'C++' started by Unforgiven, Jun 24, 2003.

  1. Unforgiven

    Unforgiven Guest

    Wenjie wrote:
    > Hello,
    >
    > In other words, will //LABELED line
    > be executed? My confusion is that the
    > object should have been destructed at
    > that time.


    Yes it will. You can't access any member variables after delete this, but it
    doesn't affect the execution of the code. The only requirement of course is
    that your object is on the heap (constructed with new), not on the stack.

    In fact, when implementing a Microsoft COM component, it's the most common
    way of implementing IUnkown::Release():

    STDMETHODIMP_(LONG) SomeObject::Release()
    {
    InternlockedDecrement(&m_nRef);
    if( m_nRef == 0 )
    {
    delete this;
    return 0; // can't access m_nRef anymore
    }
    else
    return m_nRef;
    }

    --
    Unforgiven

    "Not only do I not know the answer
    I don't even know what the question is"
    My world - Metallica
    Unforgiven, Jun 24, 2003
    #1
    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. Karl Heinz Buchegger

    Re: delete this; return ret;

    Karl Heinz Buchegger, Jun 24, 2003, in forum: C++
    Replies:
    0
    Views:
    839
    Karl Heinz Buchegger
    Jun 24, 2003
  2. Karl Heinz Buchegger

    Re: delete this; return ret;

    Karl Heinz Buchegger, Jun 25, 2003, in forum: C++
    Replies:
    0
    Views:
    695
    Karl Heinz Buchegger
    Jun 25, 2003
  3. priyanka
    Replies:
    34
    Views:
    811
    Malcolm
    Jun 4, 2006
  4. metal
    Replies:
    8
    Views:
    503
    Bruno Desthuilliers
    Oct 30, 2009
  5. X X
    Replies:
    4
    Views:
    350
    red floyd
    Jul 19, 2010
Loading...

Share This Page