Thanks for a fantastic answer and sample code.
-Dave
From: Bert Freudenberg <bert@xxxxxxxxxxxxxxx>
Reply-To: "A friendly place to get answers to even the most basic
questionsabout Squeak." <beginners@xxxxxxxxxxxxxxxxxxxxxxxxxx>
To: "A friendly place to get answers to even the most basic questions
aboutSqueak." <beginners@xxxxxxxxxxxxxxxxxxxxxxxxxx>
Subject: Re: [Newbies] terminate event?
Date: Sun, 21 Jan 2007 15:48:08 +0100 (MET)
Am Jan 21, 2007 um 15:21 schrieb Bert Freudenberg:
Am Jan 21, 2007 um 14:07 schrieb David Urquhart:
Hi
I'm a Squeak beginner. I want to write to the transcript when an object
is coming to life and when its terminating. I have an initialize method
for the birth - what is the method called that fires at death?
There is no such method. A message can be send to an object only if there
is a reference to it. As long as a reference to an object exists, it is
not dead, it does only get garbage-collected when the last reference is
removed.
About the only thing you can do is to register a *different* object to be
notified when one object is garbage-collected. This is called
"finalization".
Here's an example. Evaluate this in a workspace:
x := Object new.
x toFinalizeSend: #show: to: Transcript with: 'He''s dead, Jim!\'
withCRs
Nothing should happen. Then do
x := nil
which should print "was finalized" immediately. This is because x still
holds onto a relatively "new" object, which gets freed very fast.
However, once an object gets "old" it takes until the next full garbage
collection (GC)! Create your object again, but this time, do this:
Smalltalk garbageCollect.
x := nil.
Nothing will be printed, because the GC reclaims all space, but also marks
all surviving objects as "old". So even though after assigning nil to x
your object is dead, the finalizer does not know it, yet. Only if you
trigger a full GC again, the object's space is reclaimed, and the
finalizer is activated.
- Bert -
_______________________________________________
Beginners mailing list
Beginners@xxxxxxxxxxxxxxxxxxxxxxxxxx
http://lists.squeakfoundation.org/mailman/listinfo/beginners
|