[squeak-dev] Best practice(s) to delete an object?

Igor Stasenko siguctua at gmail.com
Wed Feb 27 01:41:13 UTC 2008


On 27/02/2008, itsme213 <itsme213 at hotmail.com> wrote:
> I have this object, X, I want to delete (typically from within the context
>  of some other object Y). So I remove it from Y (set some Y instVar to nil,
>  or remove it from some collection instVar of Y). I mark X 'deleted'.
>
>  Now I have to make sure that no other objects retain any (effective)
>  reference to X.
>
>  What are some best practices for doing this?
>

Never give out reference to X from Y, so any objects expect Y simply
can't have it. That's the best practice.

>  My current thought:
>  1. Every accessor of every instVAr for all relevant objects can do a
>  check-and-clean before returning a value.
>
>  AnyClass>>aSingleObjAccessor
>   singleInstVar isDeleted ifTrue: [singleInstVar := nil].
>   ^ singleInstVar
>
>  AnyClass>>aCollectionAccessor
>   collInstVar removeIf: [:x | x isDeleted].
>   ^collInstVar
>
>  2. Perhaps I should also use weak-refs to X wherever possible, so X could be
>  GC'd before all its incoming refs are check-and-cleaned? How does one
>  actually do this?
>
>
>  I suspect I am missing far more elegant solutions.
>
>  All pointers :-) appreciated.
>
If you raised this question, i think you missed something in design of
your app. That's the whole point of automatic memory management: you
should not care how many references point to particular object. GC
cares for you.

-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Squeak-dev mailing list