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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:27:43+00:00 2026-06-08T14:27:43+00:00

Related to Same address, multiple shared_ptr counters, is it forbidden by C++ standard? and

  • 0

Related to Same address, multiple shared_ptr counters, is it forbidden by C++ standard? and myriad other questions around multiple shared_ptr objects pointing to the same object but not sharing the underlying ref count structure.

What happens if the object inherits from “enable_shared_from_this” in the above mentioned question? What does my shared_from_this() return? One with the custom deleter or the one without?

struct B : boost::enable_shared_from_this<B> {
  boost::weak_ptr < B > get_weak() {
    return shared_from_this();
  }
};

void doNothing(B *) {
}

int main() {
  B * b0 = new B;

  boost::shared_ptr < B > sddb0(b0, doNothing);
  boost::weak_ptr < B > swddb0(sddb0->get_weak());
  //  Does this have a custom deleter???
  boost::shared_ptr < B > sddb1 = swddb0.lock();

  boost::shared_ptr < B > scdb0(b0);
  boost::weak_ptr < B > swcdb0(sddb0->get_weak());
  //  Does this *not* have a custom deleter???
  boost::shared_ptr < B > scdb1 = swcdb0.lock();
}
  • 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-08T14:27:45+00:00Added an answer on June 8, 2026 at 2:27 pm

    The deleter is associated with the owned object and will be used to dispose of it when the last owner drops its reference, so all pointers that share ownership of the same object also share the deleter. The deleter is not stored in individual shared_ptr objects, it’s stored on the heap along with the reference counts, so shared by all the objects that share the same counts.

    So let’s looks at your code. First, I had to fix your code as it didn’t even compile as shown. It’s always a good idea to test the code in your question, to avoid wasting people’s time by making them fix typos and add missing headers.

    Don’t write this:

    B * b0 = new B;
    
    boost::shared_ptr < B > sddb0(b0, doNothing);
    

    The correct way to do that is:

    boost::shared_ptr < B > sddb0(new B, doNothing);
    

    The enable_shared_from_this<B> base has a weak_ptr<B> member which gets assigned to by the shared_ptr constructor, so that weak_ptr shares ownership with sddb0, as does the shared pointer returned by shared_from_this().

    So the answer to this question is yes:

    //  Does this have a custom deleter???
    boost::shared_ptr < B > sddb1 = swddb0.lock();
    

    sddb1 shares ownership with sddb0 so has the same deleter.

    You could test that by using get_deleter, or by writing to stdout in doNothing, or by using owner_less to compare the owners of the pointers.

    This creates an object that does not share ownership with the existing shared pointers:

    boost::shared_ptr < B > scdb0(b0);

    The enable_shared_from_this<B> base’s weak_ptr<B> member gets re-assigned to by the line above, so it shares ownership with scdb0 instead of sddb0. So when you call get_weak after that point it returns a weak pointer that shares ownership with scdb0 and so doesn’t have a custom deleter. Again, you can easily verify that by seeing that doNothing isn’t called, or by using owner_less.

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

Sidebar

Related Questions

I have seen the related question here: An object with the same key already
New to development. Two position-related issues with the same Flash object: 1) After experimentation/research
After reviewing the related questions, none of them seem to address mine, so here
imagine I have a bunch of C++ related classes (all extending the same base
I am facing a dilemma related to another question but not exactly the same.
This evening I noticed a brute force attack attempt from the same IP address
I have company, customer, supplier etc tables which all have address information related columns.
First the code: class myClass(object): def __cmp__(self, other): return cmp(type(self), type(other)) or cmp(self.__something, other.__something)
This question is related to another question I asked. In my other question I
For some reason I can't get gMap to display multiple maps on the same

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.