>>finalize questions
Michael van der Gulik
squeakml at gulik.co.nz
Mon Aug 21 10:27:08 UTC 2006
Thanks, Bert.
Bert Freudenberg wrote:
>> 2. I have an object with a process "in" it. How can I make sure that
>> process is terminated when there are no more references to that object?
>
>
> Terminate the process when you release the last reference to the
> object. Like, send #release to your object before setting the ref to nil.
>
> Only as a last resort, use finalization.
I thought of a better option today: Create a "PollingService" singleton.
The process I needed was just meant to slowly poll something.
Rather than doing "[[self doSomething] repeat ] fork", I could ask the
PollingService to poll me on a regular basis. The polling service would
have something like:
PollingService>>pollObject: n
| o |
o := myWeakArray at: n.
[ o isNil ] whileFalse: [ o poll. o:= myWeakArray at: n. ].
So that when the object is garbage collected, the thread would also stop
iterating. A very useful pattern!
This could maybe even be done on the class side of that object.
I hope the small amount of time between loop iterations is enough to get
the GC to make the weak reference nil.
Michael.
More information about the Squeak-dev
mailing list
|