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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:30:01+00:00 2026-05-31T05:30:01+00:00

I am using boost::make_shared for the first time to create objects pointed to by

  • 0

I am using boost::make_shared for the first time to create objects pointed to by shared pointers. Mainly because our code was too slow and the single allocation really helped to improve performance.

After fixing some memory leaks “the hard manual way” I decided to implement a simple memory leak detector by overriding new operators for all relevant classes just for counting which objects are still alive at specific points in our application. I have implemented this several times before and was surprised to find my code no longer detects any objects.

I figured that all I had to do is override “placement new” instead of the “normal” operator new’s because of the following from the boost website documentation for make_shared:

“Effects: Allocates memory suitable for an object of type T and
constructs an object in it via the placement new expression new( pv )
T() or new( pv ) T( std::forward(args)… )
. allocate_shared
uses a copy of a to allocate memory. If an exception is thrown, has no
effect.”

My placement new is also not being called however. I have written a small test program to reproduce the behavior:

#include <iostream>
using namespace std;
#include "boost/shared_ptr.hpp"
#include "boost/make_shared.hpp"

class Test
{
public:
    Test() { cout << "Test::Test()" << endl; }

    void* operator new (std::size_t size) throw (std::bad_alloc) {
        cout << "Test new" << endl;
        return malloc(size);
    }

    void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) throw() {
        cout << "Test non-throwing new" << endl;
        return malloc(size);
    }

    void* operator new (std::size_t size, void* ptr) throw() {
        cout << "Test non-throwing placement new" << endl;
        return malloc(size);
    }
};

void* operator new (std::size_t size) throw (std::bad_alloc) {
    cout << "Global new" << endl;
    return malloc(size);
}

int main() {
    cout << "..." << endl;
    boost::shared_ptr<Test> t1(boost::make_shared<Test>());
    cout << "..." << endl;
    boost::shared_ptr<Test> t2(new Test());
    cout << "..." << endl;

    return 0;
}

Which renders the following output:

...
Global new
Test::Test()
...
Test new
Test::Test()
Global new
...

I was expecting “Test non-throwing placement new” on the 3rd line of output. What do you think the behavior should be? Do you agree that according to documentation of make_shared it should call the placement new operator of my Test class? Or did I misunderstand it?

I could copy boosts implementation locally and add a call to the placement new operator of course. But, would that be appropriate, or would it violate the intended semantics of placement new?

Thanks in advance for your time and your help.

  • 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-31T05:30:02+00:00Added an answer on May 31, 2026 at 5:30 am

    Looking as the source of make_shared, it uses the global placement new operator, instead of the new operator supplied by your class.

    ::new( pv ) T();
    

    Unfortunately (as least on OS X) (according to the standard), you cannot define your own global placement new operator. It appears that allocate_shared is more along the lines of what you’re looking for.

    Edit:

    An alternative could be to actually write a version of make_shared which uses the class’s placement new instead of the global one. It’s only about 10 lines of code, and should be fine so long as you honor the license of the original code.

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

Sidebar

Related Questions

I'm using boost's shared pointers, and enable_shared_from_this to enable returning a shared pointer to
I am using boost shared pointers in my program, and I have a class
We've pretty much moved over to using boost::shared_ptr in all of our code, however
I am using boost::any to store pointers and was wondering if there was a
I'm using boost::asio, and I have code like this: void CServer::Start(int port) { tcp::acceptor
I am getting compilation error with g++4.6 and boost 1.42 when using boost::make_shared<T>(...) ,
Are there any downsides with using make_shared<T>() instead of using shared_ptr<T>(new T) . Boost
If I have a class with a private construction, using boost::make_shared() to construct a
I am using boost serialization on windows, and I wanted to test my code
Our project is a VS2008 based project using Boost and Qt heavily. However, today

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.