Boost logo

Boost :

From: Ulrich Eckhardt (doomster_at_[hidden])
Date: 2007-11-09 01:05:53


On Sunday 04 November 2007 00:45:47 Mike Tegtmeyer wrote:
> I agree but it quickly becomes a pain because you end up writing a
> wrapper for every thing of this type to satisfy this 'wart' with
> shared_ptr:
>
> pthread_create ...
> opendir
> fopen
> open
> free
> COM objects
>
> the list goes on.

I'd take that even further, not all such resources are represented as pointer
(think filedescriptors or win32 HANDLEs), so shared_ptr doesn't help at all.
Further, you usually only need a scope-based lifetime, not true sharing with
reference counting, so std::auto_ptr or boost::scoped_ptr should be enough.
Lastly, you don't need a smart pointer at all, because the overloads of ->
and * are typically never used with those opaque handles. However, sometimes
things like returning them from functions is used, but in those cases the
behaviour of std::auto_ptr (i.e. ownership transfer) is fully sufficient.

I posted a wrapper that gives you just that some time ago here, asking for
comments. I didn't get any, probably because it was in the hot phase of the
last release. I'll post the whole mail again, the subject is "helper for
managing legacy resources".

Uli


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk