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/aeeffshzkfjbrejzt...@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


Reply via email to