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 8843623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:16:27+00:00 2026-06-14T11:16:27+00:00

I want to explicitly destroy an object (call the destructor on it and all

  • 0

I want to explicitly destroy an object (call the destructor on it and all its fields), but it may happen that I still hold some pointers to the object in question. Thus, I don’t want to yet free the memory; instead I would like to leave a sort of a flag “I am a destroyed object”.

I came with an idea of the following approach:

class BaseClass { //all objects in question derive from this class
public:
    BaseClass() : destroyed(false) {}
    virtual ~BaseClass() {}
private:
    bool destroyed;
public:
    bool isDestroyed() { return destroyed; }
    void destroy() {
        this->~BaseClass(); //this will call the virtual destructor of a derivative class
        new(this) BaseClass();
        destroyed=true;
    }
};

When destroy is called, I basically destroy the whatever object I had (perhaps a derivative one) and create a new “zombie” one in that very same place. As a result I hope to achieve:

  • Any other pointer ptr previously pointing to this object can still call ptr->isDestroyed() to verify its existence.
  • I am aware that if I don’t check the flag of the zombie and try to access fields belonging to any derived object, bad things may happen
  • I am aware that the zombie object still consumes as much memory as the destroyed object (as it may be a derivative of BaseClass)
  • I still have to free memory of the destroyed object. I hope however, that calling delete is still correct?

Questions:

Are there any other problems which I should consider when using the above pattern?

Will calling delete on the zombie object correctly free whole memory consumed by the previous (normal) object?


While I appreciate your input on how to do it differently, and I may be inclined to do it your way – I would still like to understand all the risks that the above code poses.

  • 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-06-14T11:16:28+00:00Added an answer on June 14, 2026 at 11:16 am

    You got some nasty comments to your question. Now I don’t think they are deserved although there may be better ways to do what you want. I understand where you are coming from but actually you are using the destructor the same way you would use the reset function you refuse to write. Actually you gain nothing from calling a destructor since calling a distructor has nothing to do with actually deleting or resetting anything unless you actually write the code to do it within the destructor.

    As to your question about the placement new:

    As you may know already the placement new doesn’t allocate any memory so calling it will just create the object in the same place. I understand that is exactly what you want but it’s just not ncessary. Since you don’t call delete on your object just destroy, you can set destroyed to true without initializing the class.

    To sum it up:

    1. If you use the destructor as a regular virtual function you gain nothing. Don’t do it since you can get into trouble if a destructor is called twice
    2. A call to a placement new will not allocate memory and just perform needless initialization. You can just set destroyed to true.

    To do what you want to do correctly and gain the benefits of destructors, you should overload the new and delete operators of your classes and use the normal destruction mechanism. You can then opt not to release the memory but mark it as invalid or maybe release most of the memory but leave the pointer pointing to some flags.

    EDIT

    Following the comments I decided to sum up all the risks I see and the risks that others have pointed out:

    1. Accessing invalid pointer in a multi-threaded environment: Using your method a class may be accessed after the destructor has run but before the destroyed flag is set (As to your question in one of the comments – shared_ptr is for most purposes thread safe)
    2. Relaying on a behavior you don’t totally control: Your method relies on the way destructors auto call the destructors of other members which are not dynamically allocated: This means that you still have to release dynamically allocates memory specifically, You have no control on how exactly this is implemented, You have no control on the order in which other destructors are called.
    3. Relaying on a behavior you don’t totally control (Point 2): You are relaying on the way a compiler implements the part of the destructor which calls other destructors you have no way in telling whether your code will be portable or even how will it handle calling it twice.
    4. Destructors may be called twice: Depending on your implementation this may cause memory leaks or heap corruption unless you guard against releasing the same memory twice. You claim you guard against that case by calling the placement new – However in a multi-threading environment this is not guaranteed further more you assume that all memory allocations are done by the default constructor – depending on your specific implementation this may or may not be true.
    5. You are going against the better judgment of everyone that answered your question or commented on it – You may be onto something genius but most probably you are just shooting yourself in the leg by limiting your implementation to a small subset of situations where it will work correctly. It is like when you use the wrong screwdriver you will eventually end up damaging the screw. In the same way using a language construct in a way it was not intended to be used may end up with a buggy program – Destructors are intended to be called from delete and from the code generated by the compiler to clear the stack. Using it directly is not wise.

    And I repeat my suggestion – overload delete and new for what you want

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

Sidebar

Related Questions

I have an app that I want to be launchable either explicitly when tapped
As I understand it, if I don't explicitly tell Android that I want to
I want to explicitly destroy a vector in a templated context. The following works
I want to explicitly map fields in my classes to XML, so I'm using
I want to have a profile that triggers a certain plugin(say PMD) but I
I have many cookie-cutter spring beans and don't want to explicitly define each one
I want to check which port is used, when the port is not explicitly
I am running Tomcat on a small VPS (256MB/512MB) and I want to explicitly
I have a section of an html page that I want to display in
The title may seem a little weird but what I find around the internet

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.