Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-06-26 09:52:03


I don't know if it's any help, but we had to solve some similar problems
for the Boost.Python smart pointer object (handle<T>). The problem is that
when you get a pointer from Python, there's no way to detect whether it is
a "borrowed" or a "new" reference. If it's a "borrowed" reference, you want
to increment the reference count when you build the handle; otherwise, the
count is already incremented. Most of the time it's a new reference, so the
solution we settled on was to provide a function template:

    template <class T>
    detail::borrowed<T>* borrowed(T* p){ return (borrowed<T>*)p; }

where detail::borrowed<T> is an incomplete type. The smart pointer's
constructor then detects borrowed arguments, casts them back, and
increments the reference-count. I'm beginning to think it might be best to
require explicitness always, (so you would either pass borrowed(x) or
new_reference(x) to the constructor).

Raw pointers to reference-counted objects are just too ambiguous to be
treated casually.

-Dave

----- Original Message -----
From: "Peter Dimov" <pdimov_at_[hidden]>
>
> Yes, I see the problem. In the current implementation, you have to create
an
> owning pointer to the object first, before any weak pointers, since
> counted_base interprets use_count = 0, weak_count != 0 as an indication
that
> dispose() has already been called.
>
> So it seems that you can't create weak_ptr's in the object constructor.
You
> need a shared_ptr first, and you can't create a temporary shared_ptr to
> 'this' in the constructor since it will call dispose() on block exit.


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