Soft/weak references?

Myron Alexander someone at somewhere.com
Thu Jun 21 19:52:15 PDT 2007


Sean Kelly wrote:
> BCS wrote:
>> Reply to Robert,
>>
>>> BCS Wrote:
>>>
>>>> T** ptr;
>>>>
>>> But this means there's still a reference to it.
>>>
>>
>> It's a total hack. You get a reference to a chunk of memory that 
>> contains a reference to the data, but you lie to the GC and tell it 
>> that the chunk of memory doesn't contain any references so it won't 
>> notice the refernce.
> 
> The missing piece is that this reference won't be invalidated if the 
> object is collected.  Phobos has Object.notifyRegister() for this 
> purpose, but it currently does not exist in Tango because such a 
> callback could easily deadlock the app if it enters a "synchronized" 
> block as a part of its processing.  In short, I don't know of any 
> entirely safe means of implementing a weak pointer in D.  If someone can 
> suggest one, I'd be glad to hear it.
> 
> 
> Sean

By pure coincidence, I have just discovered a need for weak references.

I am implementing chained exceptions that use property bags for storing 
additional information about the exception. Each exception class in the 
chain has it's own property bag. When I get a property, I want to search 
all the bags starting at the first exception in the chain, iterating 
through the chain until the first property with the given name is found.

At the moment, the chain is a single linked list without predecessor 
reference so a get on middle node reference will not be able to search 
the preceding node bags.

To fix this, I want to create a double linked list but for the sake of 
the GC, I want strong references in the forward direction, and weak 
references in the reverse direction.

I am using Phobos and have no intention of moving the code to Tango 
(nothing against Tango, my project is still in the baby steps phase).

If anyone could point me to a functional weak reference implementation 
for Phobos, I would be most thankful.

Thanks ahead and best regards,

Myron.


More information about the Digitalmars-d-learn mailing list