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?
And this leads to two other questions... - What is the sequence of events that occur when I create a visible object like a morph - ie when does onDraw occur exactly? - What's the best way for me to answer these sorts of questions for myself?
By the way I want to slip in a thanks to the Squeak developers and community - tuning into Squeak and smalltalk has refired my inspiration to create software.
Dave
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".
And this leads to two other questions...
- What is the sequence of events that occur when I create a visible
object like a morph - ie when does onDraw occur exactly?
The world cycle is:
1. process events for each hand 2. run all step methods 3. update screen 4. rinse and repeat
In steps 1 and 2, rectangular portions of morphs can be marked as dirty (by sending #invalidRect:). In step 3, drawing happens for those regions.
- What's the best way for me to answer these sorts of questions for
myself?
By surfing the image. Smalltalk source code is like a hypertext system, you navigate it by browsing senders and implementors of methods. Just mark some chunk of source code and press Cmd-m or Cmd- n, it usually figures out the selector you meant.
However, this only gives you a static view of course. A well-placed "self halt" and then navigating up the call chain is rather enlightening, too. Here's a recipe:
Make a subclass of, say, EllipseMorph called MyMorph. Easiest to do if you shift-right-click on EllipseMorph in a class browser (find EllipseMorph class by Cmd-F in the categories list and typing "elli") and then select "subclass template" in the shifted menu. Actually, typing over any class template might be faster ;-)
Anyway, then get the shifted menu of MyMorph and select "sample instance". You'll hold an instance of your morph in the hand. Put it somewhere.
Then add a drawOn: method to MyMorph:
drawOn: aCanvas self doOnlyOnce: [self halt]. ^super drawOn: aCanvas
"doOnlyOnce:" is essential for not getting a gazillion of debuggers. Read its comment to learn how to rearm it.
Then click your morph, a debugger should pop up, click debug, select full stack, and scroll to the very bottom. You see the complete stack trace for the UI process, the cycle I described is in WorldState>>doOneCycleNowFor:
By the way I want to slip in a thanks to the Squeak developers and community - tuning into Squeak and smalltalk has refired my inspiration to create software.
Glad to hear that :)
- Bert -
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 -
Thanks for a fantastic answer and sample code.
-Dave
From: Bert Freudenberg bert@freudenbergs.de Reply-To: "A friendly place to get answers to even the most basic questionsabout Squeak." beginners@lists.squeakfoundation.org To: "A friendly place to get answers to even the most basic questions aboutSqueak." beginners@lists.squeakfoundation.org 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@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
Excellent - thanks that was exactly the sort of guidance I hoped for. Very much appreciated.
You'll hear from me again soon :)
- Dave
From: Bert Freudenberg bert@freudenbergs.de Reply-To: "A friendly place to get answers to even the most basic questionsabout Squeak." beginners@lists.squeakfoundation.org To: "A friendly place to get answers to even the most basic questions aboutSqueak." beginners@lists.squeakfoundation.org Subject: Re: [Newbies] terminate event? Date: Sun, 21 Jan 2007 15:21:24 +0100 (MET)
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".
And this leads to two other questions...
- What is the sequence of events that occur when I create a visible
object like a morph - ie when does onDraw occur exactly?
The world cycle is:
- process events for each hand
- run all step methods
- update screen
- rinse and repeat
In steps 1 and 2, rectangular portions of morphs can be marked as dirty (by sending #invalidRect:). In step 3, drawing happens for those regions.
- What's the best way for me to answer these sorts of questions for
myself?
By surfing the image. Smalltalk source code is like a hypertext system, you navigate it by browsing senders and implementors of methods. Just mark some chunk of source code and press Cmd-m or Cmd- n, it usually figures out the selector you meant.
However, this only gives you a static view of course. A well-placed "self halt" and then navigating up the call chain is rather enlightening, too. Here's a recipe:
Make a subclass of, say, EllipseMorph called MyMorph. Easiest to do if you shift-right-click on EllipseMorph in a class browser (find EllipseMorph class by Cmd-F in the categories list and typing "elli") and then select "subclass template" in the shifted menu. Actually, typing over any class template might be faster ;-)
Anyway, then get the shifted menu of MyMorph and select "sample instance". You'll hold an instance of your morph in the hand. Put it somewhere.
Then add a drawOn: method to MyMorph:
drawOn: aCanvas self doOnlyOnce: [self halt]. ^super drawOn: aCanvas
"doOnlyOnce:" is essential for not getting a gazillion of debuggers. Read its comment to learn how to rearm it.
Then click your morph, a debugger should pop up, click debug, select full stack, and scroll to the very bottom. You see the complete stack trace for the UI process, the cycle I described is in WorldState>>doOneCycleNowFor:
By the way I want to slip in a thanks to the Squeak developers and community - tuning into Squeak and smalltalk has refired my inspiration to create software.
Glad to hear that :)
- Bert -
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
beginners@lists.squeakfoundation.org