I’ve been thinking about using shared pointers, and I know how to implement one myself–Don’t want to do it, so I’m trying std::tr1::shared_ptr,and I have couple of questions…
How is the reference counting implemented? Does it use a doubly linked list? (Btw, I’ve already googled, but I can’t find anything reliable.)
Are there any pitfalls for using the std::tr1::shared_ptr?
shared_ptrmust manage a reference counter and the carrying of a deleter functor that is deduced by the type of the object given at initialization.The
shared_ptrclass typically hosts two members: aT*(that is returned byoperator->and dereferenced inoperator*) and aaux*whereauxis a inner abstract class that contains:virtual destroy()=0;Such
auxclass (the actual name depends on the implementation) is derived by a family of templatized classes (parametrized on the type given by the explicit constructor, sayUderived fromT), that add:T*, but with the actual type: this is needed to properly manage all the cases ofTbeing a base for whateverUhaving multipleTin the derivation hierarchy)deletorobject given as deletion policy to the explicit constructor (or the defaultdeletorjust doing deletep, wherepis theU*above)A simplified sketch can be this one:
Where
weak_ptrinteroperability is required a second counter (weak_count) is required inaux(will be incremented / decremented byweak_ptr), anddelete pamust happen only when both the counters reach zero.