RCArray is unsafe

via Digitalmars-d digitalmars-d at puremagic.com
Sun Mar 1 09:48:13 PST 2015


On Sunday, 1 March 2015 at 16:54:15 UTC, Zach the Mystic wrote:
> On Sunday, 1 March 2015 at 15:44:49 UTC, Marc Sch├╝tz wrote:
>> Walter posted an example implementation of a reference counted 
>> array [1], that utilizes the features introduced in DIP25 [2]. 
>> Then, in the threads about reference counted objects, several 
>> people posted examples [3, 4] that broke the suggested 
>> optimization of eliding `opAddRef()`/`opRelease()` calls in 
>> certain situations.
>>
>> A weakness of the same kind affects DIP25, too. The core of 
>> the problem is borrowing (ref return as in DIP25), combined 
>> with manual (albeit hidden) memory management. An example to 
>> illustrate:
>>
>>    struct T {
>>        void doSomething();
>>    }
>>    struct S {
>>        RCArray!T array;
>>    }
>>    void main() {
>>        auto s = S(RCArray!T([T()])); // s.array's refcount is 
>> now 1
>>        foo(s, s.array[0]);           // pass by ref
>>    }
>>    void foo(ref S s, ref T T) {
>>        s.array = RCArray!T([]);      // drop the old s.array
>>        t.doSomething();              // oops, t is gone
>>    }
>>
>> Any suggestions how to deal with this? As far as I can see, 
>> there are the following options:
>
> See:
> http://forum.dlang.org/post/bghjqvvrdcfqmoiyyuqz@forum.dlang.org
> ...and:
> http://forum.dlang.org/post/cviwlkugnothraubcfgy@forum.dlang.org

That's not applicable here though. The compiler doesn't know 
we're doing reference counting, so it cannot insert 
AddRef/Release calls.


More information about the Digitalmars-d mailing list