RCArray is unsafe

Zach the Mystic via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 2 07:22:32 PST 2015


On Sunday, 1 March 2015 at 20:51:35 UTC, Michel Fortin wrote:
> On 2015-03-01 19:21:57 +0000, Walter Bright said:
>
>> The trouble seems to happen when there are two references to 
>> the same object passed to a function. I.e. there can be only 
>> one "borrowed" ref at a time.
>> 
>> I'm thinking this could be statically disallowed in @safe code.
>
> That's actually not enough. You'll have to block access to 
> global variables too:
>
> 	S s;
>
> 	void main() {
> 		s.array = RCArray!T([T()]);   // s.array's refcount is now 1
> 		foo(s.array[0]);           // pass by ref
> 	}
> 	void foo(ref T t) {
> 		s.array = RCArray!T([]);      // drop the old s.array
> 		t.doSomething();              // oops, t is gone
> 	}

What's the difference between that and this:

void fun() {
   T[] ta = [T()].dup;
   T* t = ta[0];
   delete ta; // or however you do it
   *t = ...;
}

Why is this a parameter passing issue and not a "you kept a 
sub-reference to a deleted chunk" issue?


More information about the Digitalmars-d mailing list