[Gc] Question about weak references

Stephane Epardaud Stephane.Epardaud at inria.fr
Tue Dec 5 00:47:31 PST 2006


Hi,

I read that the GC supports weak pointers by using 
GC_general_register_disappearing_link, is the code below something that makes 
sense to get weak pointers ?

struct weak_pointer {
   void *data;
};

struct weak_pointer* make_weak_pointer(void *data){
   struct weak_pointer* ptr =
    (struct weak_pointer*)GC_malloc_atomic(sizeof(struct weak_pointer));
   ptr->data = data;
   GC_general_register_disappearing_link(&(ptr->data), data);
   return ptr;
}

struct some_struct *s =
   (struct some_struct*)GC_malloc(sizeof(struct some_struct));
struct weak_pointer * ptr = make_weak_pointer(s);
s = NULL;
// at this point the GC _could_ collect s and set ptr->data to NULL

does this look like the way to do it ? (minus some error checking on the way)
Thanks.


More information about the Gc mailing list