[openbeos] Re: Pointer or reference?

  • From: "Bruno van Dooren" <bruno_van_dooren@xxxxxxxxxxx>
  • To: <openbeos@xxxxxxxxxxxxx>
  • Date: Thu, 1 Apr 2004 09:54:01 +0200

> Right, the owner should take care of deleting it's children.  If an object
> deletes itself then that applies that the object is the owner of itself.
> c++ programs this is never.  Thus an object in c++ should never delete
> itself.

not true. i have written code in which 1 thread can create an object that is
used by different threads
afterwards. it is possible that the thread that has created the object no
longer needs the object, but it can't be
safely deleted until everyone is finished using it.

to solve this problem, all classes that can be used this way derive from a
class 'ManagedResource'. all ManagedResource objects are managed by a
ResourceManager that tracks the objects usage through a usage counter. every
thread using the object raises the counter when it first acquires possession
of the pointer.

when the object is 'released' by the thread, the resource counter is
decremented again. the object is only deleted when the resource is marked as

all this is implemented in a threadsafe way, and i have never had any
problems with it so far.

this algorithm is similar to the COM resource tracking algorithm.

kind regards,

Other related posts: