Alex Burton wrote:
> Slight improvements might be made by only testing counted_ to know if it
> is a non null pointer instead of checking ref_ is set all the time.
This is just paranoia, of the same sort that motivates some other
posters in this thread to recommend zeroing the pointers in detach(). I
think they're right to be paranoid, because it justifies my own
You never know when some bug will cause one pointer but not the other to
be zeroed. If either is zero, we can't dereference the other, because
it's not supposed to be used any more.
An earlier iteration simplified all this by wrapping both the reference
count and the T* in a structure, and RefCountedPointer actually managed
a pointer to a structure of this type on the heap. It had the advantage
of only one data member in RefCountedPointer itself, so only one thing
to test, but it turned the single indirection of an unmanaged pointer
into triple indirection. Too expensive...