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

前端 未结 12 1977
醉酒成梦
醉酒成梦 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 10:57

    You can do it, provided its the last element in a member function, and that after return you forget that object ever existed.... but yeah, like that article asks ... Why would you want to do this ? I don't know what the standard says, but it does give me a funny feeling :P

    I guess this is a bit like should you ever use a GOTO statement, and I personally use GOTO to clean up resources in C sometimes, especially under exceptional conditions.

    I wonder what the shared state implications are (fuzzy statement I know):P

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

    Getting ready for the down votes.

    Should it: No.
    Can it Technically: Yes
    Is it a good idea: Absolutely not.
    Are there situation it is useful: Of course. If you are C++ foo is exceedingly strong. But most people are not that good. So only do this if you have a team of people working with you able to do decent code review.

    Why:
    There is no way for an object to know that it has been dynamically allocated (and thus needs deleting) or is a normal object (and thus must not be deleted) and thus how can it decidide weather it should be deleted. Thus if an object is deleting itself then in my opinion there is somthing terribly wrong with the design.

    If you have an object that needs managing then you should write a seprate object to do the management (hence smart pointers). Let the object do what it is good at, then seporate the management of the object into another object.

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

    Yes, there are a few cases where it is common.

    Reference counting:

    void release() 
    {
      cnt--;
      if (cnt == 0) 
        delete this;
    }
    

    GUI programming. In some frameworks, when a user closes a window it is common for the window to delete itself.

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

    Yes. Like all the answers say, if you're 100% sure that the class's data will not be used after the delete this is called.

    For example, in one project:

    void Test()
    MyClass * Someclass = new MyClass;
    SomeClass->DoYourThing();
    SomeClass->KillYourself();
    return;
    
    void MyClass::DoYourThing() { return; }
    void MyClass::KillYourself() {delete this;}
    

    Very simplistic explanation, the project used delete this; as part of the memory management for objects of that type; their constructor added them to a private list of classes of that type in use, and removed themselves from that list when they were destroyed and then deleted themselves (this was not in a destructor). Any objects of that class that hadn't deleted themselves when the program reached its endpoint then all had their KillYourself() equivalent called from a static member function CleanYourselves()

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

    This was often used in the MFC days. IIRC the last message a window receives is WM_NCDESTROY, at which point you could call delete this, assuming you were some form of sadist of course (although MFC itself did this at times I think.)

    0 讨论(0)
  • 2020-12-03 11:06
    1. delete this can not be called from a non member function :)
    2. It is a bad idea to do until you understand it's consequences.
    0 讨论(0)
提交回复
热议问题