Manually incrementing and decrementing a boost::shared_ptr?

前端 未结 7 1411
后悔当初
后悔当初 2021-01-05 22:57

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

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 23:34

    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); // adds ptr to the map, returns NEW id
      void release(int id) { m_objects.erase(id); }
    
    private:
      std::map< int, boost::shared_ptr > 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.

    提交回复
    热议问题