[Gc] Re: confused on weak pointers (probably asked before)

Achilleas Margaritis axilmar at otenet.gr
Sat Dec 22 03:56:33 PST 2007


O/H Achilleas Margaritis έγραψε:
> I am confused regarding weak pointers.
> 
> Which function shall I use to implement them? is it 
> 'GC_register_disappearing_link' or 'GC_general_register_disappearing_link'?
> 
> The header comment files are confusing. Some comments are below the 
> function declarations, some are above the function declarations; I don't 
> know which comments belong to which function.
> 
> I want simple weak pointer functionality: when an object is no longer 
> reachable, weak pointers to it are set to null.
> 
> Can anybody please share his wisdom on this crucial issue?

I've searched the archives and I think I've found how to implement weak 
pointers:

1) allocate a memory block which is not scanned for links with 
GC_MALLOC_ATOMIC; this block contains the weak link.

2) register the weak link allocated above with 
GC_general_register_disappearing_link(link, obj), where link points to 
the weak pointer and obj points to the weakly reachable object. The 
relationship between the link pointer and the object it points to must 
be 1:1, i.e. it should not change during the program execution. If I 
want the weak pointer to point to another object, I should allocate 
another atomic block.

3) each time I want to get a strong pointer from a weak one, I should do 
it by using the function 'GC_call_with_alloc_lock', because the weak 
pointers are NULLed without the world being stopped.

Could someone please verify the above?



More information about the Gc mailing list