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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:48:53+00:00 2026-05-28T07:48:53+00:00

Let me give you the following example: Imagine you have a window class which

  • 0

Let me give you the following example: Imagine you have a window class which can have child windows. Each child window holds a weak pointer to its parent window, each window has a list of shared ptrs to its children. Now i have the situation that if the child window is destructed, I can’t know if it is destructed because the parent window was closed, or because the child window itself was closed. Therefore the solution I came up with was to dereference the parent windows weak pointer in the child windows destructor in a try{}catch{} block to see if the parent window still exists (pseudocode):

 //we try to retain the parent window here
 try
 {
     //creates a shared pointer from the weak pointer, throws BadReferenceCounterException on fail
     WindowPtr parent = m_parentWindow.retain();

     //check if there is a parent at all, otherwise 0
     if(parent)
     {
         parent->removeChildWindow(this);
     }
  }
  catch(const BadReferenceCounterException & _ec)
  {
     std::cout<<"Catched expected exception, if Parent was closed before me!"<<std::endl;
  }

In this situation I could not think of another way to solve this simply because there is no way to know the context in which the destructor was called. Is this a stupid thing to do because I use an exception in a sort of expected situation? I know this is not a thing you want to do in performance critical code so this is not the point of the question.

Thank you!

  • 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-28T07:48:54+00:00Added an answer on May 28, 2026 at 7:48 am

    You seem to be using faulty shared/weak pointer pair. Standard (both in C++11 and in boost) shared/weak pointers allow you to do it without exceptions:

    typedef std::shared_ptr<Window> WindowPtr;
    typedef std::weak_ptr<Window> WeakWindowPtr;
    
    WeakWindowPtr m_parentWindow;
    //...
    
    WindowPtr parent = m_parentWindow.lock();
    if (parent)
    {
      // here you know parent wasn't destroyed and you can access it via parent
    }
    

    You may also want to check out code samples I’ve given in this stackoverflow answer

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

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
I found the following example to download web page(string) asynchounously: let getImage (imageUrl:string) =
I have the following example data in javascript: var variations = [ {group: 1,
I have a problem in my MATLAB code. Let me first give you some
Let's say I have 3 PHP classes: ShedBuilder, HouseBuilder and TableBuilder and the following
Let's say I have a C# type : class MyType<T> { T Value {
Let me give some code so you can see what I'm doing with the
Let me first describe my situation. I have a list of Hex values, which
Can anyone give me an example of how to use python's msilib standard library
Ok, the question might not be crystal clear. Let me give some details: Let's

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.