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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:58:55+00:00 2026-05-12T14:58:55+00:00

Is there a way to manually increment and decrement the count of a shared_ptr

  • 0

Is there a way to manually increment and decrement the count of a shared_ptr in C++?

The problem that I am trying to solve is as follows. I am writing a library in C++ but the interface has to be in pure C. Internally, I would like to use shared_ptr to simplify memory management while preserving the ability to pass a raw pointer through the C interface.

When I pass a raw pointer through the interface, I would like to increment the reference count. The client will then be responsible to call a function that will decrement the reference count when it no longer needs the passed object.

  • 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-12T14:58:56+00:00Added an answer on May 12, 2026 at 2:58 pm

    In your suggestion

    The client will then be responsible to decrement the counter.

    means that the client in question is responsible for memory management, and that your trust her. I still do not understand why.

    It is not possible to actually modify the shared_ptr counter… (hum, I’ll explain at the end how to…) but there are other solutions.

    Solution 1: complete ownership to the client

    Hand over the pointer to the client (shared_ptr::release) and expect it to pass the ownership back to you when calling back (or simply deleting the object if it is not really shared).

    That’s actually the traditional approach when dealing with raw pointers and it apply here as well. The downside is that you actually release ownership for this shared_ptr only. If the object is actually shared that might prove inconvenient… so bear with me.

    Solution 2: with a callback

    This solution means that you always keep ownership and are responsible to maintain this object alive (and kicking) for as long as the client needs it. When the client is done with the object, you expect her to tell you so and invoke a callback in your code that will perform the necessary cleanup.

    struct Object;
    
    class Pool // may be a singleton, may be synchronized for multi-thread usage
    {
    public:
      int accept(boost::shared_ptr<Object>); // adds ptr to the map, returns NEW id
      void release(int id) { m_objects.erase(id); }
    
    private:
      std::map< int, boost::shared_ptr<Object> > m_objects;
    }; // class Pool
    

    This way, your client ‘decrementing’ the counter is actually your client calling a callback method with the id you used, and you deleting one shared_ptr 🙂

    Hacking boost::shared_ptr

    As I said it is possible (since we are in C++) to actually hack into the shared_ptr. There are even several ways to do it.

    The best way (and easiest) is simply to copy the file down under another name (my_shared_ptr ?) and then:

    • change the include guards
    • include the real shared_ptr at the beginning
    • rename any instance of shared_ptr with your own name (and change the private to public to access the attributes)
    • remove all the stuff that is already defined in the real file to avoid clashes

    This way you easily obtain a shared_ptr of your own, for which you can access the count. It does not solve the problem of having the C code directly accessing the counter though, you may have to ‘simplify’ the code here to replace it by a built-in (which works if you are not multi-threaded, and is downright disastrous if you are).

    I purposely left out the ‘reinterpret_cast’ trick and the pointer offsets ones. There are just so many ways to gain illegit access to something in C/C++!

    May I advise you NOT to use the hacks though? The two solutions I presented above should be enough to tackle your problem.

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

Sidebar

Related Questions

Is there a way that I manually have a user look up the current
Is there any way to manually fire the DOMContentLoaded event? I'm trying to write
Is there a way to manually increase / decrease the timeout of a specific
Is there any way to manually create fold points in code in Eclipse? I
Is there any way to manually remove an object which the garbage collection refuses
Is there any way to manually decorate function names in MS C++? I'm going
is there a way to auto-increment the version code each time you build an
Is there a way to manually update the current location on MKMapView and have
When using Sql 2008 Change Tracking is there a way to manually clean up
Is there a way to manually trigger the code inside a LocationListener 's onLocationChanged

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.