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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:19:30+00:00 2026-05-16T06:19:30+00:00

Am I allowed to indirectly destroy object from within object’s own virtual method? Is

  • 0

Am I allowed to indirectly destroy object from within object’s own virtual method?
Is it a “defined behavior” (as long as I’m not trying to access anything after destroying the object)?
Example:

#include <memory>
#include <stdio.h>

using std::tr1::shared_ptr;

struct Child{
    virtual void selfdestruct() = 0;
    virtual ~Child(){
        fprintf(stderr, "child destroyed\n");

    }
};

typedef shared_ptr<Child> ChildPtr;

struct Parent{
    ChildPtr child;
    void clear(){
        fprintf(stderr, "clear\n");
        child = ChildPtr();
    }   
    Parent();
};

struct DerivedChild: public Child{
    Parent* parent;

    virtual void selfdestruct(){
        fprintf(stderr, "selfdestruct\n");
        if (parent)
            parent->clear();
    }

    DerivedChild(Parent* p)
    :parent(p){
    }
};

Parent::Parent(){
    child = ChildPtr(new DerivedChild(this));
}


int main(int argc, char** argv){
    Parent p;
    p.child->selfdestruct();    
    fprintf(stderr, "child is 0x%08x\n", p.child);
    return 0;
}

Output:

selfdestruct
clear
child destroyed
child is 0x00000000

If this is not a defined behavior, what can I do instead?

  • 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-16T06:19:31+00:00Added an answer on May 16, 2026 at 6:19 am

    Well, a virtual method can call delete this. After the call though, NOTHING ELSE THAT TOUCHES THAT OBJECT INSTANCE can be done, or you have invoked undefined behavior. That includes calling other methods (even non virtual methods), accessing any instance variable, or the like.

    Your specific code above invokes undefined behavior because the Child object needs a virtual destructor.

    However, any type of situation where an object needs to destroy itself is not the best of designs.

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

Sidebar

Related Questions

This should really be allowed - I do not understand why it is not.
Primitive types are not allowed in ArrayList, source . Partial solution: you can wrap
I'm trying to derive a new class from an old one. The base class
Sharepoint designer is not allowed. The forms must contain fields with references to list
We are not allowed to use IDE's in class this is being done in
I'm not allowed to use a struct with the in keyword, am I? For
Varchar(max) column not allowed to be a Primary Key in SQL Server. What is
Why is following not allowed in C++ #include <iostream> class Sample { public: void
EDIT : You're not allowed to have the word question in the title. Q-mark
Why operator [] is not allowed on std::auto_ptr? #include <iostream> using namespace std ;

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.