Enhancements can enable memory-safe reference counting
vitoroak
carvalhogvm at gmail.com
Thu May 27 20:47:44 UTC 2021
On Wednesday, 26 May 2021 at 22:06:27 UTC, tsbockman wrote:
> On Wednesday, 26 May 2021 at 21:48:40 UTC, Paul Backus wrote:
>> On Wednesday, 26 May 2021 at 18:53:21 UTC, vitoroak wrote:
>>> 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).
>>
>> In theory, these examples are fine, since they result in a
>> null dereference,
>
> No. That's what I thought at first, too, but if you walk
> through the code more carefully you will see that `x1` never
> gets set to `null`, and still points to the old target of `u1`.
> So, he is correct.
>
> I've opened [issue
> #21981](https://issues.dlang.org/show_bug.cgi?id=21981)
> requesting a fix.
I saw you mentioning breaking things in @safe code. This example
let you access an invalid pointer without no @trusted code and
heap allocation, only @safe code.
```d
struct IntRef {
int* ptr = void;
this(return scope int* p) @safe {
ptr = p;
}
int* borrow() return scope @safe {
return ptr;
}
}
void main() @safe {
import std.stdio: writeln;
auto x = 1;
auto r = IntRef(&x);
writeln(*r.borrow);
destroy!true(r);
writeln(*r.borrow);
}
```
More information about the Digitalmars-d
mailing list