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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:48:51+00:00 2026-06-16T01:48:51+00:00

I’ve read that awesome summary of Michael Burr regarding a constructor that throws an

  • 0

I’ve read that awesome summary of Michael Burr regarding a constructor that throws an exception, here: Will the below code cause memory leak in c++

My question is: Is the behavior similar when an exception is thrown by a function called from constructor? i.e. in case that the exception isn’t caught where it was thrown and thus it climbs up to the constructor and further to the function that called to the constructor.

I’m especially interested to know what about the member objects contained in that object’s class, will their destructors be called? More specifically, in my case it’s about a member of type boost::shared_ptr.

Consider the following example:

class A {
    A() { throw some_exception }
};

class B {
    B() { A a = new A(); }
};

class C {
    boost::shared_ptr<B> p2b;
    int i;
    int *pint;
    someclass objsomeclass;
    someclass* psomeclass;

public:
    C() {
        objsomeclass = someclass();
        psomeclass = new someclass();
        pint = new int(); 
        p2b(new B);
    }
};

void foo()
{
    C c();
}

main()
{
    foo();
}

Will the destructor of p2a be called?
I’ll appreciate if you could point me to an appropriate and reliable resource that covers this case.

  • 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-16T01:48:52+00:00Added an answer on June 16, 2026 at 1:48 am

    Assuming you change the code so it compiles, the destructor of p2a (now p2b) will be called because it was successfully default-constructed. However, it will still hold NULL, because your attempt to reset it in the body of C::C fails.

    The memory allocated by new B will be cleaned up automatically by the stack unwinding process. However, pint and psomeclass will both be leaked, because you’re not using RAII for these members.

    To clarify, let’s step through the code:

    C::C() {
        objsomeclass = someclass();
    
        psomeclass = new someclass();
        pint = new int();
    
        p2b.reset(new B);
        /* in your code as posted, the call sequence is:
           new B (allocate sizeof B)
             -> B::B
               -> new A (allocate sizeof A)
                 -> A::A which throws
               <- new A failed, so memory is freed
             <- B::B failed (initialized subobjects would be
                destroyed here, but there are none)
           new B failed, so memory is freed
        */
    }
    

    Note that:

    • all members are already default-initialized (because you didn’t use the initializer list), so they all get destroyed when the body of C::C unwinds
    • if psomeclass and pint were smart pointers, this would release their dynamically-allocated memory. They aren’t, so this is leaked.

    In general, it is better style to use the initializer list and RAII.

    For reference, maybe start with this (very old) article: GOTW 66

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.