Hi Johan, Be careful, it is not the object that will execute the finalizing code but a shallow copy of it. It can lead to deep debugging session sometimes ;-) o := Object new. WeakRegistry default add: o. "a shallow copy of o is registered" o := nil. Smalltalk garbageCollect. "the shallow copy of o will receive the finalize message" Cheers, Luc 2015-01-14 19:13 GMT+01:00 Johan Fabry <jfabry@dcc.uchile.cl>:
Yes, adding it to the weak registry does the trick! Thank you Esteban for the pointer and Torsten for the magic line of code WeakRegistry default add: anObject.
Being able to hook into the GC in this way is extremely cool, it saves me a lot of housekeeping work and I am very happy because of that !
On Jan 13, 2015, at 17:35, Torsten Bergmann <astares@gmx.de> wrote:
Esteban wrote:
you also need to register the object for cleanup, in the WeakRegistry. (not on my machine, sorry I cannot put an example, but look around)
It is simple and here is an example:
Create a class:
Object subclass: #Bar instanceVariableNames: 'id' classVariableNames: '' category: 'Foo'
implemented an #initialize method
initialize super initialize. id := UUID new. Transcript show: 'Initialized ', id printString; cr
and implemented a finalization method:
finalize
Transcript show: 'Finalized ', id printString; cr
Now in the workspace I instantiate the object:
|anObject| Transcript open; clear. anObject := Bar new. Transcript flush. WeakRegistry default add: anObject.
and by forcing garbage collection in a second step I can see that the finalization method is working:
3 timesRepeat: [ Smalltalk garbageCollect ]. Transcript flush.
And I get the following output
Initialized 4aa47ef1-0e4c-9a47-bddf-b406ee2042e4 Finalized 4aa47ef1-0e4c-9a47-bddf-b406ee2042e4
If it doesnt work check your VM for updates.
I'm using Pharo4.0 Latest update: #40442 on Windows with the following VM: Win32 built on Sep 4 2014 01:13:32 Compiler: 4.6.2 VMMaker versionString https://github.com/pharo-project/pharo-vm.git Commit: c7bc23dd6053d5f9934f7dd427537f76fc96eb32 Jenkins build #14851
---> Save our in-boxes! http://emailcharter.org <---
Johan Fabry - http://pleiad.cl/~jfabry PLEIAD lab - Computer Science Department (DCC) - University of Chile