The basic rules are extreme simple:
1. Objects are born with a reference count of one.
2. An object passed as a function parameter should be assumed safe
for the duration of the function.
3. If a function makes a copy of a pointer, it should do an addRef.
4. When a point to an object is no long needed, the function should
do a release and zero the pointer for safety
A case worth considering is where A creates a new object B and passes it
to C who makes a copy of the pointer. While it is faster for C to know
that A isn't going to keep its point, it is safer for C to follow the
rules and do and addRef and A to do a release following the return from
C. An even better way to handling this would be for C to create the
object itself.
Kevin argument that smart pointers are too expensive to be used with
interlocked reference counts is correct. Most usage is unnecessary but
inconsistent usage renders them next to useless.
--
Jim Starkey
President, NimbusDB, Inc.
978 526-1376
| Thread |
|---|
| • Reference Counting Conventions | Jim Starkey | 6 Oct |