CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2005
    Posts
    445

    What will happen if I say delete this

    Hi guys

    Can someone please explain what will happen if I say:
    Code:
    delete this;
    Thank you....

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: What will happen if I say delete this

    It depends entirely on the context in which you call it, and can range from exactly what you want it to do all the way to a disastrous crash.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3

    Re: What will happen if I say delete this

    Graham is correct. And actually, if you call this in a non-member function you will get a compiler error as well.

    Anyways...

    If the object was allocated on the heap... It will call it's destructor, then free it. (And that's assuming the delete call matches the new call)

    If it's an object on the stack, it will call the destructor.... And depending on the allocator used for new/delete calls when you call delete, this could crash, it might not. If this is a POD (plain old data) object, it probably will have no apparent effect if it does not crash on the delete call.

  4. #4
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: What will happen if I say delete this

    ... or you end up in an endless recursion leading to a stack overflow if you do this in the destructor itself.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  5. #5
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: What will happen if I say delete this

    Please note that accessing any member after calling delete this result in undefined behaviour.
    - Guido

  6. #6
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Thumbs up Re: What will happen if I say delete this

    The statement delete this is used in COM when an object's reference count reaches zero.
    My hobby projects:
    www.rclsoftware.org.uk

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: What will happen if I say delete this

    delete this; makes sense only when the object creation is being managed by the class itself. That is, the class creates objects of itself using new then returns to the caller and then exposes an interface that does the delete.
    Quote Originally Posted by JamesSchumacher
    And depending on the allocator used for new/delete calls when you call delete, this could crash, it might not. If this is a POD (plain old data) object, it probably will have no apparent effect if it does not crash on the delete call.
    Sorry, what does that mean?

  8. #8
    Join Date
    Dec 2005
    Posts
    445

    Re: What will happen if I say delete this

    Guys thank you very much !!!

    I think I'm starting to get it.

    Will some of you guys kindly enough, post a code examples for something that will work and the use of “delete this” is necessarily , and something that will crash the program. If it’s not too much hassle a brief explanation to the code will be great.

    Once again THANK YOU!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured