WeakDictionary and become 
Author Message
 WeakDictionary and become

Lets say we have 3 objects (A, B, and C).  A refers to B via an
instance variable, and B refers to C via and instance variable.
Also, lets assume I've anchored A somehwere through a strong reference.
Now lets store A, B and C in a WeakDictionary.

If we inspect the dictionary we see three entries for A, B, and C.  
Everything is great so far.  Now lets do the following

        B become: D

At this point I have assumed the following:

        A now references D.
        Both B and C are garbage collected (probably incorrect assumption).

However, if I inspect the WeakDictionary again, I see A, D, and C.

My question is, why didn't C get removed from the WeakDictionary?
_____________________________________________________

Software Engineer       Phone:  (508) 549-2052
The Foxboro Company     Fax:    (508) 549-6788



Sat, 24 Jul 1999 03:00:00 GMT  
 WeakDictionary and become

Quote:

> Lets say we have 3 objects (A, B, and C).  A refers to B via an
> instance variable, and B refers to C via and instance variable.
> Also, lets assume I've anchored A somehwere through a strong reference.
> Now lets store A, B and C in a WeakDictionary.

> If we inspect the dictionary we see three entries for A, B, and C.
> Everything is great so far.  Now lets do the following

>         B become: D

> At this point I have assumed the following:

>         A now references D.
>         Both B and C are garbage collected (probably incorrect assumption).

> However, if I inspect the WeakDictionary again, I see A, D, and C.

> My question is, why didn't C get removed from the WeakDictionary?
> _____________________________________________________

> Software Engineer       Phone:  (508) 549-2052
> The Foxboro Company     Fax:    (508) 549-6788

Maybe D is now referencing C?

Take a look at what I cut from the Smalltalk Archives:

Bug 8:  become:

become: is a very powerful operation.  It is easy to destroy your image
with it.  Its main use is in growing collections (see bug #1), since
it can make every reference to the old version of a collection become a
reference to the new, larger version.  It has slightly different
semantics
in Smalltalk/V and Smalltalk-80, since "x becomes: y" causes every
reference to x and y to be interchanged in Smalltalk-80, but does not
change any of the references to y in Smalltalk/V.

Suppose that you want to eliminate all references to an object x.
Saying "x becomes: nil" works fine in Smalltalk/V, but will cause
every reference to nil to become a reference to x in Smalltalk-80.
This is a sure calamity.  You want x to become a new object with no
references, such as in "x becomes: String new".

Later,

--
Daniel S. Hayes (VW 2.5)
American Management Systems (AMS)
12601 Fair Lakes Circle
Fairfax, VA  22033
703-227-5615 w
800-242-8143 x5615 (work voice mail)



Sat, 24 Jul 1999 03:00:00 GMT  
 WeakDictionary and become

On Tue, 04 Feb 1997 12:36:42 -0500, Paul Vienneau

I believe that the trouble is that a WeakDictionary maintains a
collection (a *strong* collection) of executors.  Each executor gets a
chance to perform any finalization for the newly deceased object.

The default implementation of executor (at Object) is:
        ^self shallowCopy.

Thus, the *copy* of A in the executors collection still holds C,
preventing it from being gc'd.

The easy answer is that if you do *not* require any action from the
executor, have A implement:

executor
        ^nil.

If you do require some finalization action from the executor, you'll
have to have A implement executor in such a manner that it does not
retain strong references to other objects.

Good luck,
Rob

Quote:
>Lets say we have 3 objects (A, B, and C).  A refers to B via an
>instance variable, and B refers to C via and instance variable.
>Also, lets assume I've anchored A somehwere through a strong reference.
>Now lets store A, B and C in a WeakDictionary.

>If we inspect the dictionary we see three entries for A, B, and C.  
>Everything is great so far.  Now lets do the following

>    B become: D

>At this point I have assumed the following:

>    A now references D.
>    Both B and C are garbage collected (probably incorrect assumption).

>However, if I inspect the WeakDictionary again, I see A, D, and C.

>My question is, why didn't C get removed from the WeakDictionary?
>_____________________________________________________

>Software Engineer   Phone:  (508) 549-2052
>The Foxboro Company Fax:    (508) 549-6788



Sat, 24 Jul 1999 03:00:00 GMT  
 WeakDictionary and become



Quote:
> Lets say we have 3 objects (A, B, and C).  A refers to B via an
> instance variable, and B refers to C via and instance variable.
> Also, lets assume I've anchored A somehwere through a strong reference.
> Now lets store A, B and C in a WeakDictionary.
> If we inspect the dictionary we see three entries for A, B, and C.  
> Everything is great so far.  Now lets do the following

>    B become: D

> At this point I have assumed the following:

>    A now references D.
>    Both B and C are garbage collected (probably incorrect assumption).

> However, if I inspect the WeakDictionary again, I see A, D, and C.

> My question is, why didn't C get removed from the WeakDictionary?

Reason #1: WeakDictionary holds weakly values but not keys.  You don't
explain what you mean by "store in a Dictionary", but from your "we see
three entries" I would suspect you use them as keys.

Reason #2: maybe you had references to D before the operation. #become: is
swapping references in VisualWorks, so whatever held onto D will hold onto
B afterwards.

Reason #3: garbage collector is *not required* to garbage-collect objects
as soon as they become inaccessible from system roots.  There may be some
time between losing all references to an object and actually reclaiming the
storage it occupies and fixing all the affected weak collections. GC
explicitly and then check your dictionary again.

Generally, you are asking for trouble when you play with #become: on
objects stored in hash table-based collections (Sets or Dictionaries when
the objects are keys) unless you completely understand what is going on.
It is OK as long as hash value of the object does not change.  Otherwise
the object will most probably hash onto another bucket and any further
#includes: or #at: with the object will fail.  (This does not happen in
your case, though: WeakDictionaries use #identityHash which is naturally
swapped by #become:).

--Vassili



Mon, 26 Jul 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. To become: or not to become: th

2. to become: or not to become:

3. what is a WeakDictionary?

4. [Fwd: Re: VA - WeakDictionary]

5. WeakDictionary value 0 for valid key

6. WeakDictionary

7. VA - WeakDictionary

8. WeakDictionary Bug

9. To become: or not to become: that is the question.

10. BUG: self becomes nil in the middle of a method invocation

11. WE Can Help YOU Become Solvent Again [ a d v ]

12. Become an Ebay Millionaire!

 

 
Powered by phpBB® Forum Software