From: Juan Carlos Arevalo-Baeza (jcab_at_[hidden])
Date: 2002-03-25 11:46:04
On Mon, 25 Mar 2002 11:17:01 +0100, Bjorn.Karlsson_at_[hidden] wrote:
>> From: Juan Carlos Arevalo-Baeza
>> This is why I devised a different manner of initialization
>> on my (yes, I use my own, like many others) smart pointer library.
>I find this quite interesting - what is (are) the reason(s) for rolling your own rather than using the Boost smart pointers?
Well... I developed it about 5 years ago. Considering that I haven't been aware of Boost until about a year ago, that seems reasonable to me :)
>> When a smart pointer is given a preallocated pointer, it
>> assumes that someone already owns it, and wants to share this
>> ownership, so the reference counter is initialized to 2
>> instead of 1. This gets rid of E's original problem.
>Yes, but of course, this is just a different tradeoff - if I understand the semantics of your smart pointer correctly, a common mistake would be your_shared_ptr<std::string> p(new std::string("I will never be deleted!"));
Indeed. I'm not saying it's better. In a sense, Boost promotes the fact that a mistake will cause a crash or something (it will be visible). Instead, my smart pointer will not cause a crash but a leak. Both cases can be argued.
>While this too could be alleviated by forcing the use of different initialization functions depending on the required semantics, the unfortunate side-effect is that usage (IMHO) becomes less intuitive.
A better choice might be to make the smart pointer handle the allocation. As in:
xp.New( 0 );
shared_ptr<int> xp = shared_ptr<int>().New( 0 );
where New() is a set of overloaded template member functions, for 0 to N constructor parameters.
>By the way, how is the reference counter shared between the owners when the unadorned pointer walks alone?
Actually, I've never used non-intrusive reference counting. All my reference-counted classes have the counter contained inside, and so the smart pointer becomes just a convenience class so that you don't have to deal with the counting yourself. I've never used weak references either. Instead, I've resolved cycles by making sure that there's a maximum of a cycle per object, and adding a function to the class "bool IsLastReference() const". If a piece of code owns a pointer and it's the last one, then it can get rid of it if it wants to.
I was learning C++ back then. I remember I felt really smart when I got it all working :)
ICQ: 10913692 @WORK: 101728263
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk