>>finalize questions

Bert Freudenberg bert at impara.de
Mon Aug 21 12:19:52 UTC 2006


Michael van der Gulik schrieb:
> 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.

Unlikely. Once an object gets tenured, only a full GC will collect it:

	http://minnow.cc.gatech.edu/squeak/1469

Do you really have random objects that are not owned by some specific 
object that could release them?

- Bert -



More information about the Squeak-dev mailing list