RCArray is unsafe

Michel Fortin via Digitalmars-d digitalmars-d at puremagic.com
Sun Mar 1 12:51:35 PST 2015


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
	}


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list