Passing an Object Ref
Kelly Burgess
kellyb at montana.com
Wed Dec 21 13:08:10 UTC 2022
Hi Martin,
>Does the receiving class have to calculate the $newref to the objclass or this will clear the reference to the referenced instance?
I'm not sure I understand your question.
$newref always creates a new instance and returns an object reference to it. Calculating another object reference variable as that object reference does not create another instance, it's just a copy of the pointer to the same object. While that object exists you can pass copies of the pointer around for different instances to use.
Andy's suggesting you use a task variable can simplify things - you instantiate at Startup_Task.$construct with a $newref and you dispose it in Startup_Task.$destruct with $deleteref, and you don't need to pass it as a parameter, you just use the one task variable from all instances.
If you've instantiated with $newref into an object reference task variable, and then you execute that same $newref again and overwrite the object reference, then the original instance is orphaned and hopefully garbage collected. And any copies of the first task object reference are stale and unusable.
Another way to work with a "singleton" object where you can keep the object references in instance variables is
If $clib.$objects.oTalker.$insts.$count()=0
Do $clib.$objects.oTalker.$newref() Returns iTalkerObjRef
Else
Do $clib.$objects.oTalker.$insts.$first().$objref Returns iTalkerObjRef
End If
That way you don't instantiate at startup time, the first instance that needs it creates the object, other instances can share it, and I think when there are no object references left pointing to it, Omnis will delete the object via garbage collection, so an explicit $deleteref isn't required. At least I've seen it work that way in simple tests, observing the $clib.$objects.xxx.$insts group in the notation inspector and seeing the one instance disappear after closing the last window instance that had an object reference to it.
Kind of a shotgun answer, but I wasn't sure what you're asking.
Kelly
More information about the omnisdev-en
mailing list