Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6031571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:12:12+00:00 2026-05-23T05:12:12+00:00

Possible Duplicate: Is it safe to delete this ? I’ve been doing a little

  • 0

Possible Duplicate:
Is it safe to delete this?

I’ve been doing a little work on a class that’s designed to act as a node in a linked list, and I figured I’d give the class its own deletion function as opposed to the managing class doing it. So basically it goes like this:

void Class::Delete() {
    //Some cleanup code before deleting the object
    delete this;
}

Now I’ve tested this and it appears to work fine, but I’ve had a problem in the past where objects have been in the middle of running code, been deleted, then obviously crashed the program by trying to use a no-longer-existing object.

Since “delete this” is right at the end of the function, it obviously exits the function and works fine, but is this sort of practice a bad idea at all? Could this ever blow up in my face if I’m not careful?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:12:13+00:00Added an answer on May 23, 2026 at 5:12 am

    The FAQlite answers this quite well:

    As long as you’re careful, it’s OK for
    an object to commit suicide (delete
    this).

    Here’s how I define “careful”:

    1. You must be absolutely 100% positive sure that this object was
      allocated via new (not by new[], nor
      by placement new, nor a local object
      on the stack, nor a global, nor a
      member of another object; but by plain
      ordinary new).
    2. You must be absolutely 100% positive sure that your member
      function will be the last member
      function invoked on this object.
    3. You must be absolutely 100% positive sure that the rest of your
      member function (after the delete this
      line) doesn’t touch any piece of this
      object (including calling any other
      member functions or touching any data
      members).
    4. You must be absolutely 100% positive sure that no one even touches
      the this pointer itself after the
      delete this line. In other words, you
      must not examine it, compare it with
      another pointer, compare it with NULL,
      print it, cast it, do anything with
      it.

    Naturally the usual caveats apply in
    cases where your this pointer is a
    pointer to a base class when you don’t
    have a virtual destructor.

    Basically, you need to take the same care as you do with deleteing any other pointer. However, there are more areas where things can go wrong with a member function committing suicide, compared with an explicitly-declared pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on
Possible Duplicate: Is it safe for structs to implement interfaces? Take this code: interface
Possible Duplicate: .NET Controls: Why aren’t all calls thread-safe? This question is not about
Possible Duplicate: Malloc thread-safe? I heard that glibc malloc() was not thread safe, since
Possible Duplicate: Is it safe to delete a void pointer? Will the following code
Possible Duplicate: Are C# arrays thread safe? I have been programming C# for 10
Possible Duplicate: Are C# arrays thread safe? I have a program that is single-threaded
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Malloc thread-safe? I am not a bit confused while I am reading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.