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: Kohler Markus <kohlerm@b...>
Subject: Re: Weak pointers
[...]
 
> >  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'm not sure if it makes sense to implement such a tree with weak pointers. 
To be honest, I think it's not a good idea to do so. 

The problem is that if you delete an element in the tree it may be that the 
hash table still points to the node, because there was no garbage collection 
yet. It could be possible that you would still find the entry trough the hash 
table.

IMHO the real solution is to implement a delete method for the tree whcih 
takes care of all that internal pointers. 

By the way, I know what I'm speaking about because we have here an application 
using the same idea to implement some kind of cache. I does not work properly 
because nobody knows at which point of time the garbage collector throws way 
the objects. 

Regards,
Markus
 

-- 
+----------------------------------------------------------------------------+
| Markus Kohler                          Hewlett-Packard GmbH                |
| Software Engineer                      Network & System Management Division| 
|                                        IT/E Success Team                   |
+----------------------------------------------------------------------------+