Hi,
First, let me point out that I've never used an OOP language and never used any sort of (software) garbage collection - AFAIK everything I'm about to say could be completely wrong...
cyr1x wrote:No this won't help because it could be preempted before it acquires the lock or on multiprocessor release can occur before reference.
It could be preempted before the "Reference()" function is even called.
However, how does the caller know that the object exists? Something must have told it the object exists. More importantly, something that has it's own reference must have told the caller that the object exists.
When an object is created the object creation code would increment the reference count and return a reference to the new object. Then, any time this reference is copied you'd increase the reference count *before* it's copied. For example, if threadA needs to tell threadB about the object, then it'd call the "Reference()" function and then give threadB a reference to the object.
Your problem seems to be that you're thinking about doing it the other way - e.g. give threadB a reference to the object and then call the "Reference()" function. AFAIK that will never work (unless you acquire a lock, then give threadB the reference, then let threadB call "Reference()" and then free the lock; but that's far too messy IMHO).
In this case, you don't need any locks. All you'd need is "lock inc [this->referenceCount]" in the "Reference()" function. In the "Release()" function you'd do "lock sub [this->referenceCount],1" followed by "je .delete" (or "jne .skipDelete").
The new problem is that all code must call "Reference()" before creating a copy of any reference; and if you can't trust programmers to call "free()" at the right time then you may not be able to trust them to call "Reference()" at the right time either...
Cheers,
Brendan