[Newbies] terminate event?

Bert Freudenberg bert at freudenbergs.de
Sun Jan 21 14:21:24 UTC 2007


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 -




More information about the Beginners mailing list