RC buffer

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Wed Nov 2 08:32:25 PDT 2016


On 11/02/2016 07:29 AM, Nick Treleaven wrote:
> On Wednesday, 2 November 2016 at 05:00:23 UTC, Andrei Alexandrescu wrote:
>> In order to make opAssign safe, a language change will be necessary.
>
> Technically, it should be possible with runtime checks:
>
> https://forum.dlang.org/post/aeeffshzkfjbrejztksj@forum.dlang.org
>
> The checking overheads disappear when -noboundschecks is passed. The
> user has to manually copy the RCSlice when necessary for correct code.

It seems opAssign for RCSlice is unsafe, is that right? Consider (with 
your codebase):

@safe void fun(ref RCSlice!int a, ref RCSlice!int b)
{
     a = RCSlice!int();
     ... use b ...
}

@safe void gun(ref RCSlice!int s)
{
     fun(s, s);
}

Assume the reference count of s is 1 upon entering gun. Then the first 
thing opAssign does is to call the destructor of the slice, which 
deallocates memory. Subsequently the other reference (b) is dangling.


Andrei


More information about the Digitalmars-d mailing list