Global reference counts in multi-threaded code

I read that WeakRef allows testing to see if an object has been destroyed. Is there any way to force an object’s destruction even if referenced?

you would need to set property in owner webpage/window to nil.

WeakRef has an object property, which you can test for nil.

Thanks Christian. What if there are threads running with the global object assigned to local variables?

Well, if a local variable is cleaned up on the end of the method, the object’s reference count is decreased by one.
If it reaches zero, then there are no more references and so it gets released.

Yes but if it switches out in a loop to run another thread before the object is destroyed, and the other thread deals with the object so its no longer relevant, how can it be destroyed?
Checking at the top of every boundary for a quit property in various objects isn’t very nice.

No.

Thanks Tim. This is sad because I’ve got a memory leak because of it (I think).

No. Not because of a WeakRef.

You probably have somewhere a circular reference.
e.g. window -> control -> data object -> window/control

I had a misconception about WeakRef, that assignments made via a weakref did not increase the reference count. This misconception impacts the structure of my code since in effect, I was using weakref as a late binding mechanism with dictionaries which it is not. Assigning to local vaiables at the beginning of a loop and setting the local variable to nil at the end of the loop is one way out. However while I have only seen context switches at the boundaries of top level not nested loops, I don’t know how reliable that is and such coding is inelegant. I think I will try making a local copies of objects instead or implement my own reference counting checked at the top of loops.

Internally Xojo keeps some data structures to manage weak references.
Not sure how, but either each object has an optional list of weak refs for it, or they manage a global list for that to know which WeakRef points to which object.
Anyway, in an object destructor they can walk over the list and clear all WeakRefs.