Re: Weak pointers

From: Damien Doligez (
Date: Mon Mar 17 1997 - 20:19:00 MET

Date: Mon, 17 Mar 1997 20:19:00 +0100
From: Damien Doligez <>
Message-Id: <>
Subject: Re: Weak pointers

>From: Frank Christoph <>
> 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

This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:09 MET