[squeak-dev] Re: finalizing processes?

Andreas Raab andreas.raab at gmx.de
Thu Dec 1 08:43:13 UTC 2011


On 12/1/2011 0:32, Yoshiki Ohshima wrote:
>    Hello,
>
> I have trouble terminating some processes I make.  More specifically,
> I'm trying to terminate some processes when the their associated
> "owner" object is garbage collected.  The problem seems that these
> processes hold back pointers to my owner object via outerContext of
> the closures so the owner object does not get garbage collected when
> nobody else is pointing to it.
>
> Attached is my test case.  After filing in this to a trunk image,
> evaluate:
>
> 	k := KEventStream new.
> 	KEventStream beTimer: k with: 100.
>
> in a workspace.  You'll see a little counter going on the left edge of
> screen.  What I want to do is to stop the counter by evaluating:
>
> 	k := nil. Smalltalk garbageCollect.
>
> but the processes hold onto the closure, which in turn holds onto the
> context for executing #beTimer:with:, and that context holds onto 'k',
> or 'anObject'.
>
> I thought that closure could just be detached from the outer context but
> it is not the case here.  Why?

Just guessing: Is it possibly because you're using a shared variable 
(sema) in #beTimer:with:? In order to reference the shared variable from 
the process the outer scope needs to be retained. You'd have to pass the 
variable into the process to avoid requiring the outer context for sema, 
along the lines of:


	timerProcess := [:sema|
		[true] whileTrue: [(Delay forMilliseconds: interval) wait. sema signal]
	] newProcessWith: {semaphore}.
	timerProcess priority: Processor timingPriority.
	timerProcess resume.

Cheers,
   - Andreas



More information about the Squeak-dev mailing list