using weak collections

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

using weak collections

Yoandy Rodríguez Martínez

Hello again:

I need to create an object's registry that keeps certain objects in a  
dictionary and a second registry that keeps only objects currently in the  
first registry so i thought it was a good idea to user IdentityDictionary  
and WeakIdentityKeyDictionary so when the any object in the first registry  
is removed then the second registry remove's it too.
Problem is that when i try to test my code, the second assertion fails. Is  
there anything wrong with my test or weak collections are not suppose to  
work like that?

ColonyTest>>testWeakRefs
|registry anotherRegistry object |
registry:= Registry uniqueInstance.
"this keeps weak references"
anotherRegistry := Colony new.
object:= ColonyMember new.
registry register: anotherRegistry.
anotherRegistry addMember: object.
registry register: object.
"object is into the second registry"
self assert:[ anotherRegistry contains: object].
"if we unregister this reference then the registry should lose it too"
registry unregister: object.
self deny:[ anotherRegistry contains: object]

_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: using weak collections

Marcus Denker-4

On Mar 24, 2010, at 6:50 PM, Yoandy Rodríguez Martínez wrote:

>
> Hello again:
>
> I need to create an object's registry that keeps certain objects in a dictionary and a second registry that keeps only objects currently in the first registry so i thought it was a good idea to user IdentityDictionary and WeakIdentityKeyDictionary so when the any object in the first registry is removed then the second registry remove's it too.
> Problem is that when i try to test my code, the second assertion fails. Is there anything wrong with my test or weak collections are not suppose to work like that?
>

Yes. But doesn't your method hold on to the object, too? It's pointed to by the "object" temp, which is not weak.

> ColonyTest>>testWeakRefs
> |registry anotherRegistry object |
> registry:= Registry uniqueInstance.
> "this keeps weak references"
> anotherRegistry := Colony new.
> object:= ColonyMember new.
> registry register: anotherRegistry.
> anotherRegistry addMember: object.
> registry register: object.
> "object is into the second registry"
> self assert:[ anotherRegistry contains: object].
> "if we unregister this reference then the registry should lose it too"
> registry unregister: object.
> self deny:[ anotherRegistry contains: object]
>




--
Marcus Denker  -- http://www.marcusdenker.de
INRIA Lille -- Nord Europe. Team RMoD.


_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users