Smart
pointer programming techniques
Using incomplete classes for implementation hiding
The "Pimpl" idiom
Using abstract classes for implementation hiding
Preventing delete px.get()
Using a shared_ptr to hold a pointer to an array
Encapsulating allocation details, wrapping factory
functions
Using a shared_ptr to hold a pointer to a statically
allocated object
Using a shared_ptr to hold a pointer to a COM object
Using a shared_ptr to hold a pointer to an object
with an embedded reference count
Using a shared_ptr to hold another shared
ownership smart pointer
Obtaining a shared_ptr from a raw pointer
Obtaining a shared_ptr (weak_ptr)
to this in a constructor
Obtaining a shared_ptr to this
Using shared_ptr as a smart counted handle
Using shared_ptr to execute code on block
exit
Using shared_ptr<void> to hold an arbitrary
object
Associating arbitrary data with heterogeneous shared_ptr
instances
Post-constructors and pre-destructors
Using shared_ptr as a CopyConstructible mutex lock
Using shared_ptr to wrap member function calls
Delayed deallocation
Weak pointers to objects not managed by a shared_ptr
A proven technique (that works in C, too) for separating interface from implementation is to use a pointer to an incomplete class as an opaque handle:
class FILE; FILE * fopen(char const * name, char const * mode); void fread(FILE * f, void * data, size_t size); void fclose(FILE * f);
It is possible to express the above interface using shared_ptr,
eliminating the need to manually call fclose:
class FILE; shared_ptr<FILE> fopen(char const * name, char const * mode); void fread(shared_ptr<FILE> f, void * data, size_t size);
This technique relies on shared_ptr's ability to execute a custom
deleter, eliminating the explicit call to fclose, and on the fact
that shared_ptr<X> can be copied and destroyed when X
is incomplete.
A C++ specific variation of the incomplete class pattern is the "Pimpl" idiom.
The incomplete class is not exposed to the user; it is hidden behind a
forwarding facade. shared_ptr can be used to implement a "Pimpl":
// file.hpp:
class file
{
private:
class impl;
shared_ptr<impl> pimpl_;
public:
file(char const * name, char const * mode);
// compiler generated members are fine and useful
void read(void * data, size_t size);
};
// file.cpp:
#include "file.hpp"
class file::impl
{
private:
impl(impl const &);
impl & operator=(impl const &);
// private data
public:
impl(char const * name, char const * mode) { ... }
~impl() { ... }
void read(void * data, size_t size) { ... }
};
file::file(char const * name, char const * mode): pimpl_(new impl(name, mode))
{
}
void file::read(void * data, size_t size)
{
pimpl_->read(data, size);
}
The key thing to note here is that the compiler-generated copy constructor,
assignment operator, and destructor all have a sensible meaning. As a result,
file is CopyConstructible and Assignable,
allowing its use in standard containers.
Another widely used C++ idiom for separating inteface and implementation is to
use abstract base classes and factory functions. The abstract classes are
sometimes called "interfaces" and the pattern is known as "interface-based
programming". Again, shared_ptr can be used as the return type of
the factory functions:
// X.hpp:
class X
{
public:
virtual void f() = 0;
virtual void g() = 0;
protected:
~X() {}
};
shared_ptr<X> createX();
-- X.cpp:
class X_impl: public X
{
private:
X_impl(X_impl const &);
X_impl & operator=(X_impl const &);
public:
virtual void f()
{
// ...
}
virtual void g()
{
// ...
}
};
shared_ptr<X> createX()
{
shared_ptr<X> px(new X_impl);
return px;
}
A key property of shared_ptr is that the allocation, construction, deallocation,
and destruction details are captured at the point of construction, inside the
factory function. Note the protected and nonvirtual destructor in the example
above. The client code cannot, and does not need to, delete a pointer to X;
the shared_ptr<X> instance returned from createX
will correctly call ~X_impl.
delete px.get()It is often desirable to prevent client code from deleting a pointer that is
being managed by shared_ptr. The previous technique showed one
possible approach, using a protected destructor. Another alternative is to use
a private deleter:
class X
{
private:
~X();
class deleter;
friend class deleter;
class deleter
{
public:
void operator()(X * p) { delete p; }
};
public:
static shared_ptr<X> create()
{
shared_ptr<X> px(new X, X::deleter());
return px;
}
};
shared_ptr to hold a pointer to an arrayA shared_ptr can be used to hold a pointer to an array allocated
with new[]:
shared_ptr<X> px(new X[1], checked_array_deleter<X>());
Note, however, that shared_array is
often preferable, if this is an option. It has an array-specific interface,
without operator* and operator->, and does not
allow pointer conversions.
shared_ptr can be used in creating C++ wrappers over existing C
style library interfaces that return raw pointers from their factory functions
to encapsulate allocation details. As an example, consider this interface,
where CreateX might allocate X from its own private
heap, ~X may be inaccessible, or X may be incomplete:
X * CreateX(); void DestroyX(X *);
The only way to reliably destroy a pointer returned by CreateX is
to call DestroyX.
Here is how a shared_ptr-based wrapper may look like:
shared_ptr<X> createX()
{
shared_ptr<X> px(CreateX(), DestroyX);
return px;
}
Client code that calls createX still does not need to know how the
object has been allocated, but now the destruction is automatic.
shared_ptr to hold a pointer to a statically
allocated objectSometimes it is desirable to create a shared_ptr to an already
existing object, so that the shared_ptr does not attempt to
destroy the object when there are no more references left. As an example, the
factory function:
shared_ptr<X> createX();
in certain situations may need to return a pointer to a statically allocated X
instance.
The solution is to use a custom deleter that does nothing:
struct null_deleter
{
void operator()(void const *) const
{
}
};
static X x;
shared_ptr<X> createX()
{
shared_ptr<X> px(&x, null_deleter());
return px;
}
The same technique works for any object known to outlive the pointer.
shared_ptr to hold a pointer to a COM ObjectBackground: COM objects have an embedded reference count and two member
functions that manipulate it. AddRef() increments the count. Release()
decrements the count and destroys itself when the count drops to zero.
It is possible to hold a pointer to a COM object in a shared_ptr:
shared_ptr<IWhatever> make_shared_from_COM(IWhatever * p)
{
p->AddRef();
shared_ptr<IWhatever> pw(p, mem_fn(&IWhatever::Release));
return pw;
}
Note, however, that shared_ptr copies created from pw will
not "register" in the embedded count of the COM object; they will share the
single reference created in make_shared_from_COM. Weak pointers
created from pw will be invalidated when the last shared_ptr
is destroyed, regardless of whether the COM object itself is still alive.
shared_ptr to hold a pointer to an object
with an embedded reference countThis is a generalization of the above technique. The example assumes that the
object implements the two functions required by intrusive_ptr,
intrusive_ptr_add_ref and intrusive_ptr_release:
template<class T> struct intrusive_deleter
{
void operator()(T * p)
{
if(p) intrusive_ptr_release(p);
}
};
shared_ptr<X> make_shared_from_intrusive(X * p)
{
if(p) intrusive_ptr_add_ref(p);
shared_ptr<X> px(p, intrusive_deleter<X>());
return px;
}
shared_ptr to hold another shared
ownership smart pointerOne of the design goals of shared_ptr is to be used in library
interfaces. It is possible to encounter a situation where a library takes a shared_ptr
argument, but the object at hand is being managed by a different reference
counted or linked smart pointer.
It is possible to exploit shared_ptr's custom deleter feature to
wrap this existing smart pointer behind a shared_ptr facade:
template<class P> struct smart_pointer_deleter
{
private:
P p_;
public:
smart_pointer_deleter(P const & p): p_(p)
{
}
void operator()(void const *)
{
p_.reset();
}
P const & get() const
{
return p_;
}
};
shared_ptr<X> make_shared_from_another(another_ptr<X> qx)
{
shared_ptr<X> px(qx.get(), smart_pointer_deleter< another_ptr<X> >(qx));
return px;
}
One subtle point is that deleters are not allowed to throw exceptions, and the
above example as written assumes that p_.reset() doesn't throw. If
this is not the case, p_.reset() should be wrapped in a try {}
catch(...) {} block that ignores exceptions. In the (usually
unlikely) event when an exception is thrown and ignored, p_ will
be released when the lifetime of the deleter ends. This happens when all
references, including weak pointers, are destroyed or reset.
Another twist is that it is possible, given the above shared_ptr instance,
to recover the original smart pointer, using
get_deleter:
void extract_another_from_shared(shared_ptr<X> px)
{
typedef smart_pointer_deleter< another_ptr<X> > deleter;
if(deleter const * pd = get_deleter<deleter>(px))
{
another_ptr<X> qx = pd->get();
}
else
{
// not one of ours
}
}
shared_ptr from a raw pointerSometimes it is necessary to obtain a shared_ptr given a raw
pointer to an object that is already managed by another shared_ptr
instance. Example:
void f(X * p)
{
shared_ptr<X> px(???);
}
Inside f, we'd like to create a shared_ptr to *p.
In the general case, this problem has no solution. One approach is to modify f
to take a shared_ptr, if possible:
void f(shared_ptr<X> px);
The same transformation can be used for nonvirtual member functions, to convert
the implicit this:
void X::f(int m);
would become a free function with a shared_ptr first argument:
void f(shared_ptr<X> this_, int m);
If f cannot be changed, but X has an embedded
reference count, use make_shared_from_intrusive
described above. Or, if it's known that the shared_ptr created in
f will never outlive the object, use a null deleter.
shared_ptr (weak_ptr)
to this in a constructor[...]
class X
{
public:
X()
{
shared_ptr<X> this_(???);
}
};
[Not possible in general. If X can have automatic or static
storage, and this_ doesn't need to keep the object alive, use a null_deleter.
If X is supposed to always live on the heap, and be managed by a
shared_ptr, use:]
class X
{
private:
X() { ... }
public:
static shared_ptr<X> create()
{
shared_ptr<X> px(new X);
// use px as 'this_'
return px;
}
};
shared_ptr to this[Sometimes it is needed to obtain a shared_ptr from this in a virtual member function.]
[The transformations from above cannot be applied.]
class X
{
public:
virtual void f() = 0;
protected:
~X() {}
};
class Y
{
public:
virtual shared_ptr<X> getX() = 0;
protected:
~Y() {}
};
// --
class impl: public X, public Y
{
public:
impl() { ... }
virtual void f() { ... }
virtual shared_ptr<X> getX()
{
shared_ptr<X> px(???);
return px;
}
};
[Solution:]
class impl: public X, public Y
{
private:
weak_ptr<impl> weak_this;
impl(impl const &);
impl & operator=(impl const &);
impl() { ... }
public:
static shared_ptr<impl> create()
{
shared_ptr<impl> pi(new impl);
pi->weak_this = pi;
return pi;
}
virtual void f() { ... }
virtual shared_ptr<X> getX()
{
shared_ptr<X> px = weak_this.lock();
return px;
}
};
[Future support planned, impl: public enable_shared_from_this<impl>.]
shared_ptr as a smart counted handle[Win32 API allusion]
typedef void * HANDLE; HANDLE CreateProcess(); void CloseHandle(HANDLE);
[Quick wrapper]
typedef shared_ptr<void> handle;
handle createProcess()
{
shared_ptr<void> pv(CreateProcess(), CloseHandle);
return pv;
}
[Better, typesafe:]
class handle
{
private:
shared_ptr<void> pv_;
public:
explicit handle(HANDLE h): pv_(h, CloseHandle) {}
HANDLE get() { return pv_.get(); }
};
shared_ptr to execute code on block exit[1. Executing f(p), where p is a pointer:]
shared_ptr<void> guard(p, f);
[2. Executing arbitrary code: f(x, y):]
shared_ptr<void> guard(static_cast<void*>(0), bind(f, x, y));
shared_ptr<void> to hold an arbitrary
object[...]
shared_ptr<void> pv(new X);
[Will correctly call ~X.]
[Can be used to strip type information: shared_ptr<X> ->
(shared_ptr<void>, typeid(X))]
shared_ptr
instances[...]
typedef int Data; std::map< shared_ptr<void>, Data > userData; // or std::map< weak_ptr<void>, Data > userData; to not affect the lifetime shared_ptr<X> px(new X); shared_ptr<int> pi(new int(3)); userData[px] = 42; userData[pi] = 91;
[...]
class X
{
public:
X();
virtual void postconstructor();
virtual void predestructor() throw();
~X() throw();
struct deleter
{
void operator()(X * p)
{
p->predestructor();
delete p;
}
}
static shared_ptr<X> create()
{
shared_ptr<X> px(new X, X::deleter());
px->postconstructor(); // can throw
return px;
}
};
shared_ptr as a CopyConstructible mutex lock[Sometimes it's necessary to return a mutex lock from a function. A noncopyable lock cannot be used.]
class mutex
{
public:
void lock();
void unlock();
};
shared_ptr<mutex> lock(mutex & m)
{
m.lock();
return shared_ptr<mutex>(&m, mem_fn(&mutex::unlock));
}
[Or to encapsulate it in a dedicated class:]
class shared_lock
{
private:
shared_ptr<void> pv;
public:
template<class Mutex> explicit shared_lock(Mutex & m): pv((m.lock(), &m), mem_fn(&Mutex::unlock)) {}
};
[Usage:]
shared_lock lock(m);
[Note that shared_lock is not templated on the mutex type, thanks
to shared_ptr<void>'s ability to hide type information.]
shared_ptr to wrap member function calls[http://www.research.att.com/~bs/wrapper.pdf]
template<class T> class pointer
{
private:
T * p_;
public:
explicit pointer(T * p): p_(p)
{
}
shared_ptr<T> operator->() const
{
p_->prefix();
return shared_ptr<T>(p_, mem_fn(&T::suffix));
}
};
class X
{
private:
void prefix();
void suffix();
friend class pointer<X>;
public:
void f();
void g();
};
int main()
{
X x;
pointer<X> px(&x);
px->f();
px->g();
}
[In some situations, a single px.reset() can trigger an expensive
deallocation in a performance-critical region.]
class X; // ~X is expensive
class Y
{
shared_ptr<X> px;
public:
void f()
{
px.reset();
}
};
[Solution 1]
vector< shared_ptr<void> > free_list;
class Y
{
shared_ptr<X> px;
public:
void f()
{
free_list.push_back(px);
px.reset();
}
};
// periodically invoke free_list.clear() when convenient
[Solution 2, as above, but use a delayed deleter]
struct delayed_deleter
{
template<class T> void operator()(T * p)
{
try
{
shared_ptr<void> pv(p);
free_list.push_back(pv);
}
catch(...)
{
}
}
};
shared_ptrMake the object hold a shared_ptr to itself, using a null_deleter:
class X
{
private:
shared_ptr<X> this_;
int i_;
public:
explicit X(int i): this_(this, null_deleter()), i_(i)
{
}
// repeat in all constructors (including the copy constructor!)
X(X const & rhs): this_(this, null_deleter()), i_(rhs.i_)
{
}
// do not forget to not assign this_ in the copy assignment
X & operator=(X const & rhs)
{
i_ = rhs.i_;
}
weak_ptr<X> get_weak_ptr() const { return this_; }
};
When the object's lifetime ends, X::this_ will be destroyed, and
all weak pointers will automatically expire.
$Date$
Copyright © 2003 Peter Dimov. Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.