Login  Register

Re: Finalization question

Posted by Ben Coman on Aug 02, 2014; 1:33pm
URL: https://forum.world.st/Finalization-question-tp4771533p4771541.html

Udo Schneider wrote:

> All,
>
> I just stumbled over something which thought was pretty easy - turned
> out it isn't as easy as I thought or I'm simply _______ (fill in
> expression of choice like "too dumb", "blind", etc.)
>
> I have an object which starts a background process to do it's job.
> This object holds a reference to the running proccess in an instance
> variable.
>
> My goal is to #terminate that proccess once the object isn't needed
> anymore (before GC).
>
> Using WeakRegistry for the object itself doesn't work. E.g.
>
> MyObject>>#initialize
>     super initialize.
>     WeakRegistry default register: self.
>     myProcess := self startProcess.
>
> MyObject>>#finalize
>     myProcess terminate. "Doesn't work. myProcess is already nil"
>
> I think this is due to all the ivars of the object already being nil
> when #finalize is begin called. As far as I read the code this is due
> to Object>>#executor returning a shallow copy of the object and thus
> being by intention ...
>
> So how to proceed? Did I miss something obvious? Is overwriting
> #executor the way to go (in terms of "does it work" and "doesn't it
> break something")?
>
> Best Regards,
>
> Udo
>
>
>
>
>

I don't know much about finalization of Weak constructs, but maybe an
alternative.  I assume you have something like..
startProcess
    [    
        "do stuff"  
    ] fork.

so how about...
startProcess
    [ [
        "do stuff"
         myProcess = nil.
    ] whileFalse ] fork.

cheers -ben