[Newbies] Re: Proper object removal

Bert Freudenberg bert at freudenbergs.de
Wed Jun 4 13:47:46 UTC 2008


On 04.06.2008, at 15:20, Rob Rothwell wrote:

> On Wed, Jun 4, 2008 at 8:41 AM, Klaus D. Witzel <klaus.witzel at cobss.com 
> > wrote:
> {'this ', 'and ', 'that'} collect: [:each | ].
> Smalltalk garbageCollect.
> {thisContext tempAt: 1} inspect
>
> first line: create some object and make a temp var point to it.
> second line: invoke GC.
> third line: see what's still pointed to by the temp var.
>
>
> So...if I do line 1, then line 3, should I see items in the Array,  
> or just the Array itself.
>
> This is all very interesting, because in my application I am  
> creating lots of collections of objects maintaining parent/child  
> pointers, and I have obviously just done it WRONG!
>
> Chasing pointers and objects is showing me that when I remove  
> objects from my collections, I need to do that "all the way down," I  
> think...


Normally you do not need to do this "all the way down". Say you have a  
tree of objects, where the parent points to its children, and the  
children each back to its parent. Then it is sufficient to remove one  
child to have the whole subtree rooted at the child go away. There is  
no need to "delete" all the children's children. Also, the "back  
pointers" do no harm. But it is important that there are no other  
references to these objects - no inspectors, debuggers, etc.

As soon as there is no "path" from a global object to your object, it  
is subject to garbage collection (which will happen later, not  
immediately). The #allInstances method still finds these "unreachable"  
objects, because it simply looks at each and every object that was not  
yet gc'ed.

- Bert -




More information about the Beginners mailing list