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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:48:33+00:00 2026-06-07T01:48:33+00:00

Boost’s make_shared() function promises to be exception-safe while attempting to create a shared_ptr .

  • 0

Boost’s make_shared() function promises to be exception-safe while attempting to create a shared_ptr.

Why is there no make_scoped() equivalent? Is there a common best practice?

Here’s a code example from the boost::scoped_ptr documentation that seems unsafe to me:

    boost::scoped_ptr<Shoe> x(new Shoe);

This line of code will do these three things in order:

  • Allocate heap memory for Shoe
  • Call the constructor for Shoe
  • Call the constructor for boost::scoped_ptr<Shoe>

If the constructor for Shoe throws an exception, memory will be leaked. (see R. Martinho Fernandes answer) The scoped_ptr won’t handle the deallocation because it hasn’t been constructed yet.

Is this an oversight? Or is there a solution that I’ve failed to notice?

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

    If the constructor fails, no memory is leaked. That’s part of the semantics of new, no smart pointers involved:

    struct Foo { Foo() { throw 23; } };
    new Foo(); // no memory leaked
    

    The added exception safety provided by make_shared comes from when you’re initializing two shared_ptrs in an expression and the two initializations are not sequenced, as is the case in function call arguments:

    struct Bar {
        Bar(bool fail) {
            if(fail) throw 17;
        }
    }
    f(shared_ptr<Bar>(new Bar(true)), shared_ptr<Bar>(new Bar(false)));
    

    Since there is no sequencing between the evaluations of new Bar(true), shared_ptr<Bar>(new Bar(true)), new Bar(false) and shared_ptr<Bar>(new Bar(false)), the following could happen:

    1. new Bar(false) is evaluated and succeeds: memory is allocated;
    2. new Bar(true) is evaluated and fails: it doesn’t leak memory resulting from this evaluation;

    No shared_ptr was constructed at this time, and so the memory allocated in #1 is now leaked.

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

Sidebar

Related Questions

Whenever boost's numeric_cast<> conversion fails, it throws an exception. Is there a similar template
Boost::Serialization has builtin support for boost::shared_ptr<> . Is there a way to use this
boost::bind overloads several operators for its placeholders: For convenience, the function objects produced by
boost::shared_ptr has an unusual constructor template<class Y> shared_ptr(shared_ptr<Y> const & r, T * p);
Boost throws an exception. An unhandled exception 0x000007fefd0aa88d in the svg.exe: Exception Microsoft C:
Boost.Asio documentation suggests the following exception handling pattern : boost::asio::io_service io_service; ... for (;;)
boost::tuple has a get() member function used like this: tuple<int, string, string> t(5, foo,
is boost::thread object thread-safe? Should I lock calling of member methods of boost::thread (e.g.
Is boost::make_shared obsolete now? Haven't found its definition in 1.35.
boost::shared_polymorphic_downcast and the other boost::shared_ptr functions reside in <boost/shared_ptr.hpp> I recently enabled support for

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.