RC buffer

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Wed Nov 2 09:02:26 PDT 2016


On 11/02/2016 11:32 AM, Andrei Alexandrescu wrote:
> 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.

Ah, never mind, the two names refer to the same object. -- Andrei




More information about the Digitalmars-d mailing list