RAII is misleading

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Tue Nov 24 09:03:46 PST 2015


On Tuesday, 24 November 2015 at 16:12:23 UTC, duff wrote:
> On Tuesday, 24 November 2015 at 15:14:16 UTC, duff wrote:
>> I've discovered this when working on a kind of "fat pointer" 
>> system.
>>
>> The "RAII" term is misleading, it tends to let people think 
>> that the one who initializes a resource is its owner.
>>
>> The reality is different. The real owner of a resource is the 
>> one who see it "as valid" for the first time.
>
> You would like to serialize a tree of object ?
>
> Using ReferenceCounting: which one has to write the properties 
> of an object that's only a reference ?
>
> Using ReferenceCounting: let's say that the last object who has 
> a referenced object serialiazes it, there's no guarantee that 
> when the soft run again his reference is already set...
>
> The only way to do this correctly is to use system of ownership 
> rather than RC system.

One approach is to start serialization at the root node, annd 
whoever sees a node first could serialize it, and all references 
it contains, recursively. When an object is serialized, its 
memory address or unique ID of some kind could be added to a 
hashtable. Then, during recursive serialization, if you found an 
object which was already in the table, you wouldn't serialize it 
again. If we were talking about C++, I would say to use the 
object address as the unique ID - shared_ptr<T>::get(). Im not 
sure if D's RefCounted has an equivalent function.

For deserializing, you could do two passes. First, read all nodes 
into a table, which you can index by the node's old memory 
address, and second, build the tree and resolve referenes to the 
new nodes' addresses.

I'm not claiming this is the best approach, but it has worked for 
me in the past.

    Bit


More information about the Digitalmars-d mailing list