How does a reference-counting smart pointer’s reference counting work?

I’ve seen two different non-intrusive approaches to this:

  1. The smart pointer allocates a small
    block of memory to contain the
    reference counter. Each copy of the
    smart pointer then receives a
    pointer to the actual object and a
    pointer to the reference count.
  2. In addition to an object pointer,
    each smart pointer contains a
    previous and next pointer, thereby
    forming a doubly-linked list of
    smart pointers to a particular
    object. The reference count is
    implicit in the list. When a smart
    pointer is copied, it adds itself to
    the list. Upon destruction, each
    smart pointer removes itself from
    the list. If it’s the last one in
    the list it then frees the
    referenced object as well.

If you go here and scroll to the bottom, there is an excellent diagram which explains these methods much more clearly.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)