[Q][Newbie] disappearing dependents?

willem van den ende squeak at willemvandenende.com
Fri Nov 14 15:51:39 UTC 2003


On Friday 14 November 2003 17:15, Boris Gaertner wrote:
<snip>
> > http://www.willemvandenende.com/RssClient.st
> >I put the index.rss file I used in the unit tests here:
> > http://www.willemvandenende.com/index.rss
>
> Willem, you are right, you loose a dependence. This is of course a mistake,
> but it is not a silly mistake, but one that I saw never before. I recommend
> this example to all those that are really interested in the MVC metaphor
> (I hope you will kindly allow that those interested can download your
> example.)
Sure, this piece of code is under the same license as squeak :-) 

<snip>
> The dependents are kept in an instance of DependentsArray and that is
> a *week* subclass of Array. 

I had noticed that dependents in Object were a very special kind of global 
weak collection, that is why I introduced the Model subclass. I didn't notice 
the collection there was also weak. Thanks for pointing that out!

<snip>

> Your mistake is that you do not store the instance of RssViewer elsewhere
> and therefore it is garbage collected at a moment that you cannot foresee,
> but certainly when you save the image.

Seems like a sensible time for a gc...

>
> What can you do:
> You have different options.
> 1. You can establish a strong reference to the instance of RssViewer.
> That is simple, make it the model of  the top window (add the line that
> I put between arrows):
<snip> 
This removes the problem, but looks like a hack and it is one.

The RssViewer feels more like a Morph or a View than it feeks like a Model (it 
instantiates and positions a Window, a PluggableList and a Scamper...). I 
stole some of the code from Scamper, which also makes itself a model of the 
SystemWindow. 

I chose this solution, since it involves less code than modifying 
addDependents, and the lifespan of the RssViewer is equal to that of the 
SystemWindow it is contained in.

<snip>
> 2. You may conclude, that RssModel should not keep its dependents in
> a weak collection. Again a bit of magic is sufficient to force RssModel
> to store its dependents in an Array. You add the instance method
>
> addDependent: anObject
<method body snipped>
> Compare this with Object>>addDependent: to see that
> a call copyWithDependent: was replaced with a call of
> copyWith:  
I see, we prevent a DependentsArray which is a weak subclass of Array 
(interesting construction) from being initialized here.

>I think this is what we had in earlier versions
> of Model (in Squeak 2.7 for example), before we changed
> to weak arrays.

The current solution prevents memory leaks, which is nice if you keep your 
image open for a long time.
>
>
> These are two ways to fix the problem. I think (and hope!)
> that some subscribers will find time to tell us something about
> the correct use of weak collections. I think you programmed against
> a pattern that is implicitly introduced when we use weak collections
> but we should make the rules explicit. (I feel that this is more
> difficult and that I need more time to write down the rules. Perhaps
> others will contribute their insights)

I am curious as well, maybe this is not the preferred way to use observer?

Thanks Boris, it is nice to get two solutions :-) .  I hope to find some time 
next week to add a list of feeds to the reader, so it is basically 
functional.

greetings,

-- 
Willem van den Ende - http://www.cq2.nl
Meet CQ2 at XP-DAY Benelux http://www.xpday.nl



More information about the Squeak-dev mailing list