Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: Weak pointers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Damien Doligez <Damien.Doligez@i...>
Subject: Re: Weak pointers
>From: Frank Christoph <christo@nextsolution.co.jp>
>  Does this mean that even a full weak pointer can be erased?

Yes.  An empty pointer doesn't contain anything, so talking about
erasure is only meaningful for full pointers.

> What does "at any time" mean --- even if the pointer is still
>accessible from the root set, its contents can be erased?

Yes, but if that object is still reachable through some normal
(non-weak) pointer (i.e. any object except a weak array), then the GC
will not erase that weak pointer.  In other words, the GC will only
erase a weak pointer if this erasure makes the object unreachable,
thus deallocatable.

>  Is this intended to contrast with a usual reference, which must
>always be initialized?

I think initialisation is irrelevant.

>  Are weak pointers intended to model C pointers?

No.  Weak pointers are essentially a GC feature: pointers that are not
considered as making the pointed object reachable.

>  Could someone post an example of their use?

You can use them to implement hash-consing.  Assume you have some tree
data structure.  If you want to share the nodes wherever possible, you
can put every created node into a hash table.  Then when you are about
to create a new node, you first look in the hash table to see if the
same node has already been created.  If this is the case, you return
the old one from the hash table instead of creating a new one.

But the above prevents the GC from collecting the nodes when they
become unused, because they will always be reachable from the hash
table.  If you use weak pointers in your hash table, the GC will be
able to deallocate the dead nodes, but you still can have access to
the live ones through the weak pointers.

I don't have the weak hash table code yet, but it will certainly
be in the library at some point in the future.

Hope this helps.  If it's not clear, please ask more questions.

-- Damien