Re: Implementation of shared_ptr
Minkoo Seo wrote:
I've got a question on the implementation of std::tr1::shared_ptr. In
the book titled Beyond C++ Standard Library, a simple technique to
prevent the deletion of raw pointer of shared_ptr is presented:
[...]
class A [virtual protected dtor]
class B: public A [virtual public dtor]
[...]
shared_ptr<A> a(new B());
A *raw_a = a.get();
delete raw_a;
In this example, delete raw_a raises a compile time error because A's
destructor is protected. However, the following successfully compiles
and run:
int main()
{
shared_ptr<A> a(new B());
return EXIT_SUCCESS;
}
This means that, in shared_ptr, something like the following is
happening:
B *b = dynamic_cast<B*>(a.get());
delete b;
No, it's slightly more complicated. The thing is also hinted at in the
documentation (well, at least in the origin at Boost's): shared_ptr<> holds
a deleter object that knows the type passed to the constructor. That means
that even if you
shared_ptr<void> p(new B);
it will remember that it points to a B and invoke delete on the B pointer
passed to it. The thing that would happen in a primitive smart pointer is
to call delete on a void pointer, invoking undefined behaviour.
I searched for the source code, but I could not even find the
destructor. Moreover, AFAIK, shared_ptr is implemented like:
template<typename T>
class shared_ptr
{
T *t_;
public:
shared_ptr(T *t): t_(t) { }
template<typename Y>
shared_ptr(Y *y): t_(y) { }
...
};
The second construtor, which takes Y *y as an argument, is provided for
the situation where type conversion can happen like the one I've shown
above (storing new B() into shared_ptr<A>). And this implies that the
type information of Y is completely lost after y is assigned to t_.
No, your assumption about the implementation is wrong. The shared_ptr holds
not only a pointer to the object, but also a pointer to a reference counter
and a functor for deleting the contained object.
If you take a look at a shared ptr object in a debugger, its internal
structure should become a bit clearer, at least clear enough to then find
the relevant codeparts. Also be sure to look at the Boost documentation, it
contains quite a few hints about how shared_ptr can be used - it is much
more complex than it looks on first sight.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]