Should “delete this” be called from within a member method?

前端 未结 12 1978
醉酒成梦
醉酒成梦 2020-12-03 10:55

I was just reading this article and wanted SO folks advice:

Q: Should delete this; be called from within a member method?

相关标签:
12条回答
  • 2020-12-03 11:09

    Yes you can and here's a good explanation of when and why

    0 讨论(0)
  • 2020-12-03 11:12

    I think there are really 2 questions here

    Can delete this be validly called from a member method?

    Yes. This is legal as long as you are very careful with the usage.

    Should delete this be used within a member method?

    In very specific cases this is necessary. Certain types of smart pointers for instance use the delete this pattern to kill the pointer. Examples: CComPtr<> style.

    Other than smart pointers though, it should be avoided unless you have a very good reason for doing this. Even then, I would carefully reconsider my scenario and see if there was a way around it.

    0 讨论(0)
  • 2020-12-03 11:17

    Not without a very good reason to do so.

    The problem is that when you call delete this in a member function, you're creating a nasty side effect - the caller still has a reference to your instance that is now completely invalid.

    This is probably not an expected behavior, so it could easily lead to nasty bugs.

    That being said, there are times when this is appropriate (I've seen some memory management schemes, with certain libraries, where you explicitly create methods in the classes that delete themselves - primarily for language interoperability). In general, I think it's bad practice, though.

    0 讨论(0)
  • 2020-12-03 11:17

    Although not directly related to this thread i wanted to clarify this. I was asked a question that given a situation:

    int* a = new int ;
    int* b = a ;
    delete a;
    

    Now is the next statement safe?

    cout<<*b ;

    My answer: After delete a, the location pointed to by a has been marked for deletion and at any point of time it can be assigned to some other object. Hence accessing the value using b is not safe as it may get modified after being allocated to some other object.

    Note: No downvoting please, this is just a clarification

    0 讨论(0)
  • 2020-12-03 11:21

    Normally this is a bad idea, but it's occasionally useful.

    It's perfectly safe as long as you don't use any member variables after you delete, and as long as clients calling this method understand it may delete the object.

    A good example of when this is useful is if your class employs reference counting:

    void Ref() {
      m_References++;
    }
    
    void Deref() {
      m_References--;
      if (m_References == 0) {
        delete this;
      }
    }
    
    0 讨论(0)
  • 2020-12-03 11:22

    Some threading libraries use it when implementing an auto destroy on thread termination.

    void Thread::threadFunc()
    {
        doRun();
    
        if(this->destroyOnExit == true)
            delete this;
    }
    
    0 讨论(0)
提交回复
热议问题