[Issue 22680] @safe hole with destructors

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jan 17 13:43:12 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=22680

--- Comment #3 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to ag0aep6g from comment #2)
> (In reply to RazvanN from comment #1)
> > There is nothing unsafe in assigning a class reference to another.
> 
> Unless you're assigning garbage, which is happening here. A more elaborate
> demonstration of the unsafety:
> 
> ----
> import std.stdio: writeln;
> import core.memory: GC;
> C c;
> class C
> {
>     immutable int* ip;
>     this(int x) @safe { this.ip = new int(x); }
>     ~this() @safe { c = this; }
> }
> void main() @safe
> {
>     () { new C(42); } ();
>     () { ubyte[1000] clear_stack; } ();
>     () @trusted { GC.collect(); } ();
>     immutable int* ip = c.ip;
>     writeln(*ip); /* Prints "42". */
>     new int(13);
>     int should_still_be_42 = *ip;
>     writeln(should_still_be_42); /* Prints "13" - immutable data has
> changed. */
> }
> ----

My comment was unfinished. What I wanted to propose is to mark destructors that
are @safe with scope. I don't think there would be any un-wanted side effects.

I added some labels and I pushed "Save Changes", but I forgot that I had a
comment started.

--


More information about the Digitalmars-d-bugs mailing list