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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:16:33+00:00 2026-06-18T08:16:33+00:00

The problem below is not very common, so I can’t find anything online that

  • 0

The problem below is not very common, so I can’t find anything online that could be of help:

I have class parent, which must have the default constructor and destructor, I am not supposed to define anything about these two members.

I need to take control of ptr destruction, it must be destructed only if it is not referenced, which is controlled by counter in someclass.

class parent {
public:
   someclass * ptr;
}
class child: public parent {

   ~child() {                           //<-- this line
      cout<<"in ~child!"<<endl;
      if(this->ptr == NULL)  cout<<"ptr is NULL"<<endl;
      this->ptr->delete_reference();    //<-- here ptr = NULL
    }

    someclass* get_ptr() {
       return this->ptr;
    }
}

class someclass {
  void delete_reference() {
    counter--;
    if(counter == 0)   delete this;
}

The problem is that in this line parent’s default destructor is called and does ptr = NULL, as a result, ptr is not properly destructed.

Is there a way to skip the call of ~parent with these restrictions?

I also tried to remove inheritance, and have an instance of parent in child, but the same thing happens, parent’s destructor is called again.

Furthermore, I need ptr in parent because there are more child classes and they need to share this information.

Thank you!

EDIT:
OK as I understood, I got something wrong here… the problem is how ptr gets NULL.
As long as I confused the order of destructor calls, then the problem is not in this line.

void function() {
  child ch1;
  for(int i = 0; i<1; i++) {
    cout<<iterating<<endl; 
    do_some_work(ch1);
  }
  if(ch1->get_ptr() != NULL) cout<<"ptr is OK"<<endl;

  return;
  //Here is the destructor ~child called (?)
}

int main() {
  function();

}

The above calling is the way I use child. Inside do_some_work there is actually a lot of work that is difficult to describe here.

The point is that after do_some_work finishes, ptr is OK. Then I suppose destructors are called and ptr seems to be NULL.

So, the above will print:

iterating
ptr is OK
in child
ptr is NULL
Segmentation fault

Is there any explanation after this edit?

Thank you all very much!

  • 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-18T08:16:34+00:00Added an answer on June 18, 2026 at 8:16 am

    The problem is that in this line parent’s default destructor is called

    No.

    and does ptr = NULL,

    And No.

    First, the parent’s destructor (default or otherwise) is called after the child’s, not before.

    Second, the the parent’s default destructor probably doesn’t set ptr to NULL. It probably doesn’t touch ptr at all. (I say “probably”, because there is no way of knowing. Referencing ptr at all after the parent destructor returns is undefined behavior.)

    I suggest that you have come to an incorrect conclusion. Please post a short complete program that demonstrates your problem and we can help you arrive at the correct conclusion.

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

Sidebar

Related Questions

hi im have a very common problem but unable to find any answer i
i have a problem with below's code, onProgressUpdate is not running ... however the
I understand that this problem is incredibly common and I have read through quite
Can anybody give me some help for figuring out the problem below. Here are
I'm surely not the first one noticed the problem below: when using the graphical
I am having a problem with the below code. I am not sure why
What is the problem with my code below? It's not returning any items even
The code below is working, but with a problem I do not understand. When
I had a weird problem here. Please check the code below. I do not
UPDATE: I've realized that the below problem only occurs if the user has already

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.