Search code examples
c++shared-ptrraii

C++: reference counter for objects


I need a reference counter for an object not allocated on the heap.

I need it to implement a RAII mechanism on objects that cannot be easily copied and destructed:

class File
{
private:
    int fd;
public:
    File( const std::string &path ) ... // opening file
    destroy( );                         // actually closing file

    File( const File &f ) ...           // just copying the fd
    ~File( );                           // doing nothing
}

For a scenario like this a std::shared_ptr is usually used: the constructor and the destructor of the object whose pointer is shared are called only once.

In my case, however, I'd prefer avoiding allocating the object on the heap. I'd need a shared_object class that does a job similar to std::shared_ptr, so that my class' non-copy-constructor and destroy function (in the example above) are called only once.

Does anything like this exist?


Solution

  • If you want to have shared pointer behavior while allocating nothing in dynamic storage ("on the heap") you can look at various smart pointer implementation strategies. In Modern C++ Design, the author discusses many of these strategies in the "Smart Pointers" chapter, which is freely (and legally) available online.

    The technique you will be interested in is reference linking. Using this technique, the smart pointer objects are linked together in a bi-directional doubly linked list instead of pointing to a dynamically allocated reference counter.


    All that being said, using a std::shared_ptr, std::unique_ptr or their Boost variants will probably be much faster to write and easier to maintain. If dynamic allocation and the reference count are ever a bottleneck (I doubt it will be, but then we can't generalize too hastily), you can always take time to use a custom reference linking version instead.