[Newbies] Re: Proper object removal
Klaus D. Witzel
klaus.witzel at cobss.com
Wed Jun 4 08:32:51 UTC 2008
On Wed, 04 Jun 2008 09:55:11 +0200, Norbert Hartl wrote:
> On Wed, 2008-06-04 at 00:04 -0400, Rob Rothwell wrote:
>> Hello,
>>
>> After much help already, I think I need some training in proper object
>> removal.
>>
>> When my application creates an object and stores it in an
>> OrderedCollection, and than wants to delete it, I am trying to do so
>> quite explicitly with something like:
>>
>> DataManager>>deleteSelectedAbstractors
>> self selected do: [:each |
>> self abstractors remove: each.
>> each := nil.
>> ]
>>
>> which removes the object from my application (it's collection), and
>> yet when I look for my object in the system with
>>
>
>> DataAbstractor allInstances.
>>
>> I still see the object I removed, even with an explicit Smalltalk
>> garbageCollect or garbageCollectMost. Anything I create just seems to
>> hang around forever.
>>
>> Any help understanding what I need to do to make sure my objects
>> really go away when I am done with them would be greatly appreciated!
>
> The objects are still referenced in the collection you get
> from self selected. The line with "each := nil" is useless
> as each is only a temporary variable.
Not 100% useless, since temporary variables (and arguments, for that
matter) survive any attempt, from within the same method, to garbage
collect them:
{'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.
/Klaus
> I assume that you want
> to empty the selected collection as well. you could do
>
> DataManager>>deleteSelectedAbstractors
> self selected copy do: [:each |
> self abstractors remove: each.
> self selected remove: each.
> ]
>
> regards,
>
> Norbert
More information about the Beginners
mailing list