Enhancements can enable memory-safe reference counting
vitoroak
carvalhogvm at gmail.com
Wed May 26 18:53:21 UTC 2021
On Friday, 14 May 2021 at 00:45:09 UTC, tsbockman wrote:
> [snip]
>
> I think D is very close to being able to sanely express `@safe`
> reference counting APIs. I don't think `@live` is necessary;
> rather, we just need to complete `scope` and `return` and fix
> some RAII related bugs. For performance reasons, move operators
> and some minor changes to the GC would also be good, but are
> not actually required.
>
> Destroy?
Every time I tried to do something similar in D I stumbled across
the same problems and as far as I know it's not possible to
implement it completely @safe today. I think one of the problems
is that you can manually destroy/move any struct while there are
still references/pointers to it or its internals like in the
example below (I used your borrow mixin template).
```d
void receiveByValue(Unique!(int*) u) @safe {
}
void main() @safe {
import std.stdio: writeln;
auto u1 = Unique!(int*)(true);
mixin borrow!(u1, "x1");
writeln(*x1); // ok
destroy(u1);
writeln(*x1); // should not be possible
import core.lifetime: move;
auto u2 = Unique!(int*)(true);
mixin borrow!(u2, "x2");
writeln(*x2); // ok
receiveByValue(move(u2));
writeln(*x2); // should not be possible
}
```
I don't know how this could be solved but for me it's a blocker
to do a @safe Unique or RC type. Maybe if I always return an
RCRef or something like this but I think the overhead would be
too big.
More information about the Digitalmars-d
mailing list