weak pointers, cbdata, refcounting

From: Alex Rousskov <rousskov@dont-contact.us>
Date: Mon, 22 Aug 2005 16:38:05 -0600

Hi there,

        I need to implement what some call a "weak pointer". A weak pointer
becomes null or otherwise invalid when the corresponding object goes
away or is invalidated. For an example, please see


I cannot use existing RefCount.h for weak pointing because the interface
does not support "invalidation" of a pointer -- the RefCount pointer
stays valid until nobody uses it.

I seem to be unable to use cbdata because that interface does not seem
to work well with non-trivial classes(*). Please correct me if I am

I can add an invalidate()/isInvalid() pair to RefCount<> so that folks
can "invalidate" a RefCount pointer. This is very simple, but does not
catch cases where folks forget to check for validity before
de-referencing the pointer. It also makes RefCount do things besides

Alternatively, I can add a new WeakPtr<> class to accompany RefCount<>,
similar to weak_ptr interface at the above Boost URL. This is a lot more
work (than adding an "invalid" bit to the existing RefCount<>). Is that
the best option? Is there a better way?

Thank you,

(*) For example, multiple inheritance does not seem to be supported by
cbdata due to (void*) casting:

        class B {...};
        class A {...};
        class C {...};
        class ABC: public A, public B, public C { ...};

        ABC *abc = new ABC;
        B *b = abc;
        assert(abc != b);
        if (cbdataReferenceValid(b)) // fails the cbdata cookie check

Destructors are probably not called either, for similar reasons.
However, I see classes that are declared with CBDATA_CLASS and that have
destructors. Are all those destructors empty? Am I missing something?
Received on Mon Aug 22 2005 - 16:40:43 MDT

This archive was generated by hypermail pre-2.1.9 : Wed Aug 31 2005 - 12:00:06 MDT