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

  • Home
  • SEARCH
  • 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 6090135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:10:45+00:00 2026-05-23T12:10:45+00:00

I have one class ( A ) which knows when it should be deleted.

  • 0

I have one class (A) which knows when it should be deleted. I believe an object cannot directly delete itself (i.e., you can’t call a destructor from inside it) so I’ve used a callback.

Here is the pseudo-code of my current solution:

class A {
  Owner* owner; // is set in constructor

  A(Owner* ow) : owner(ow) {}
  void willDeleteOurself() {
    owner->deleteObject();
  }
} 

class Owner {
  A* obj;

  Owner() { // e.g. constructor
    obj = new A(this);
  }

  void deleteObject() {
    delete obj;
  }
}

I have an object of class Owner which owns an object of class A. Now, at some point, obj “knows” that it should be deleted, and a call of willDeleteOurself() occurs. This code seems to work.

Now, my question is this: Is this a safe way to delete obj?

I’m concerned because when the method deleteObject returns (reaches }), it jumps back to the end of willDeleteOurself(), but the object from whence that method was called has been destroyed.

Is this construct safe when I have no more statements referencing obj after the call to owner->deleteObject();?

Update:

Thanks for the answers. I simplified a bit here. I do not have A* obj;, but a boost smartpointer (alone and sometimes in a map) to the object. I think if I call delete this on the wrapped object, the smartpointer will not recognize that the underlying object is deleted. Is that correct?

As delete this (and also my code above) is “dangerous”, is there a safer way to solve my problem?

  • 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-23T12:10:46+00:00Added an answer on May 23, 2026 at 12:10 pm

    An object cannot directly delete itself

    There is no particular reason why an object cannot delete itself, you have to be careful not to access any member after the object has been deleted, but there are circumstances where it makes sense.

    I have done that with some fire-and-forget objects encapsulating a thread or timer. They were not really owned by any other object, so they might be created dynamically and leaked until they complete their work. Then they would call delete this; as the very last operation.

    Adding intermediate steps from the point where you need (or want) to delete the object and the delete code itself will only make things more complex and harder to maintain. The following code:

    void willDeleteOurself() {
       delete this;
       // [1]
    }
    

    is much more explicit and easy to read: from this point on, the object is dead, you cannot use it, do not add code in [1], because the object is dead now. If on the other hand, you disguise the delete through the callback, then others might see the code and not realize that adding code to [1] is dangerous.

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

Sidebar

Related Questions

I have a class Person which can have several Homes, each one with one
I have an IPAddress class which has one property named ip and in its
In one of my applications I have a class which is responsible for user
I have a service class which has overloaded constructors. One constructor has 5 parameters
I have a class User with one field called birthDate which is a java.sql.Date
I have a datagridview in which one column contains a custom class, which I
You have a method on a class that has 2 parameters, one of which
I have a base class, B, which has two constructors, one with no paremeters
I have done a few projects lately using a Database Object super class which
I have a model called Contact which has_one guest like so. class Contact <

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.