views:

215

answers:

3
+13  Q: 

shared_ptr magic :)

Mr. Lidström and me had an argument :) Mr. Lidström's claim is that a construct shared_ptr<Base> p(new Derived); doesn't require Base to have a virtual destructor.

@Daniel: Really? Will the shared_ptr clean up correctly? Could you please in this case demonstrate how that effect could be implemented? – Armen Tsirunyan

@Armen: The shared_ptr uses its own destructor to delete the Concrete instance. This is known as RAII within the C++ community. My advice is that you learn all you can about RAII. It will make your C++ coding so much easier when you use RAII in all situations. – Daniel Lidström

@Daniel: I know about RAII, and I also know that eventually the shared_ptr destructor may delete the stored px when pn reaches 0. But if px had static type pointer to Base and dynamic type pointer to Derived, then unless Base has a virtual destructor, this will result in undefined behavior. Correct me if I am wrong. – Armen Tsirunyan

@Armen: The shared_ptr knows the static type is Concrete. It knows this since I passed it in its constructor! Seems a bit like magic, but I can assure you it is by design and extremely nice. – Daniel Lidström

So, gentlemen, jugde us. How is it possible (if it is) to implement shared_ptr without requiring polymorphic classes to have virtual destructor? Thanks in advance

+6  A: 

Simply,

shared_ptr uses special deleter function that is created by constructor that always uses the destructor of the given object and not the destructor of Base, this is a bit of work with template meta programming, but it works.

Something like that

template<typename SomeType>
shared_ptr(SomeType *p)
{
   this->destroyer = destroyer_function<SomeType>(p);
   ...
}
Artyom
hmm... interesting, I am starting to believe this :)
Armen Tsirunyan
@Armen Tsirunyan You should have peeked into the design description of the shared_ptr before starting the discusson. This 'capture of the deleter' is one of the essential features of shared_ptr...
paul_71
@paul_71: I agree with you. On the other hand I believe this discussion was useful not only for me, but also for other people that didn't know this fact about the shared_ptr. So I guess it was not a great sin to start this thread anyway :)
Armen Tsirunyan
@Armen Of course not. Rather, you did a good job in pointing to this really very very important feature of shared_ptr<T> which is frequently overseen even by experienced c++ developers.
paul_71
+8  A: 

When shared_ptr is created it stores a deleter object inside itself. This object is called when the shared_ptr is about to free the pointed resource. Since you know how to destroy the resource at the point of construction you can use shared_ptr with incomplete types. Whoever created the shared_ptr stored a correct deleter there.

For example, you can create a custom deleter:

void DeleteDerived(Derived* d) { delete d; } // EDIT: no conversion needed.

shared_ptr<Base> p(new Derived, DeleteDerived);

p will call DeleteDerived to destroy the pointed object. The implementation does this automatically.

ybungalobill
+1 for the remark about incomplete types, very handy when using a `shared_ptr` as an attribute.
Matthieu M.
+5  A: 

Yes, it is possible to implement shared_ptr that way. Boost does and the upcoming standard also requires this behaviour. As an added flexibility shared_ptr manages more than just a reference counter. A so-called deleter is usually put into the same memory block that also contains the reference counters. But the fun part is that the type of this deleter is not part of the shared_ptr type. This is called "type erasure" and is basically the same technique used for implementing the "polymorphic functions" boost::function or std::function (in C++0x) for hiding the actual functor's type. To make your example work, we need a templated constructor:

template<class T>
class shared_ptr
{
public:
   ...
   template<class U>
   explicit shared_ptr(U* ptr);
   ...
};

So, if you use this with your classes Base and Derived ...

class Base {};
class Derived : public Base {};

int main() {
   shared_ptr<Base> sp (new Derived);
}

... the templated constructor with U=Derived is used to construct the shared_ptr object. The constructor has thus the chance to create the appropriate deleter object and reference counters and stores a pointer to this control block as a data member. If the reference counter reaches zero, the previously created and Derived-aware deleter will be used to dispose the object.

sellibitze