Re: Implementation of shared_ptr
Minkoo Seo ??????????????:
Hi group!
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:
#include <iostream>
#include <tr1/memory>
using namespace std;
using namespace std::tr1;
class A
{
protected:
virtual ~A() { }
};
class B: public A
{
public:
virtual ~B() { }
};
int main()
{
shared_ptr<A> a(new B());
A *raw_a = a.get();
delete raw_a;
return EXIT_SUCCESS;
}
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;
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_.
That being the case, shared_ptr<A> a(new B()) can not deallocate the
memory because the deletition of a raw pointer of type A is a compile
time error.
So, I'm curious how shared_ptr is handling the deallocation of Y *y.
Could anybody give me some pointers?
Here is the simplest version of shared_ptr implementation I can think
of.
I omitted many important methods because it's not important in this
question.
It is different from tr1::shared_ptr implementation. The
tr1::shared_ptr has more layers, more methods, broken compiler
considerations, deleter object, ... In short, it's far more complex.
But you can see some internal mechanism of shared_ptr from this code.
The smart_ptr should keep type inforamtion to delete correctly. So it
newed the 'impl<T>' instance using impl_base pointer. When smart_ptr is
deleted, it calls the virtual destructor of impl_base. And finally, it
deletes the impl::U* not smart_ptr::T*.
struct impl_base
{
impl_base(int c) : cnt(c) { }
virtual ~impl_base() { }
int cnt;
};
template <class U>
struct impl : impl_base
{
impl(U* p) : impl_base(1), ptr(p) { }
~impl() {
delete ptr;
}
U* ptr;
};
template <class T>
class smart_ptr
{
public:
smart_ptr(T *p) : impl_(new impl<T>(p)), ptr_(p) { }
template<typename Y>
smart_ptr(Y *p) : impl_(new impl<Y>(p)), ptr_(p) { }
~smart_ptr() {
--impl_->cnt;
if (impl_->cnt == 0) delete impl_;
}
T* get() const {
return ptr_;
}
private:
impl_base* impl_;
T* ptr_;
};
HTH,
iwongu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]