Bug 23561 – std.typecons.Unique!struct does not destroy struct instance

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2022-12-15T19:29:46Z
Last change time
2022-12-24T12:41:00Z
Keywords
pull
Assigned to
No Owner
Creator
Nick Treleaven

Comments

Comment #0 by nick — 2022-12-15T19:29:46Z
void main() { import std.typecons; int i; struct S { ~this() { i++; } } { Unique!S u = new S; } assert(i == 1); // fails } Unique.~this calls destroy on the S*, not the struct instance: if (_p !is null) { destroy(_p); _p = null; }
Comment #1 by vital.fadeev — 2022-12-16T04:48:04Z
import std.stdio; static int i; struct S { ~this() { i++; writeln("DTOR: i == ", i ); } } void main() { { auto u = new S; u.destroy(); // (*u).destroy(); // works fine! } import core.memory; GC.collect(); writeln("EXIT: i == ", i ); // 0, but except 1 assert(i == 1); // fails } // Output: // EXIT: i == 0 // DTOR: i == 1 // // core.exception.AssertError@source\app.d(24): Assertion failure Confirm. Also failed. FAIL: Destructor S, possible not called. WANTED: Wanted to call S.~this(). PURPOSE: smart-pointer. std.typecons.Unique!S for auto close handle. DMD: DMD32 D Compiler v2.101.0-dirty, Windows 10, x64 DMD: LDC 1.30, Windows 10, x64
Comment #2 by vital.fadeev — 2022-12-16T04:48:59Z
But works when S is class: class S { ~this() { i++; writeln("DTOR: i == ", i ); } }
Comment #3 by dlang-bot — 2022-12-16T13:17:00Z
@ntrel created dlang/phobos pull request #8651 "Fix Issue 23561 - std.typecons.Unique!struct does not destroy struct …" fixing this issue: - Fix Issue 23561 - std.typecons.Unique!struct does not destroy struct instance https://github.com/dlang/phobos/pull/8651
Comment #4 by dlang-bot — 2022-12-24T12:41:00Z
dlang/phobos pull request #8651 "Fix Issue 23561 - std.typecons.Unique!struct does not destroy struct …" was merged into master: - 8df49f52fd8a36a2247e15cc47e8b165e0dd485f by Nick Treleaven: Fix Issue 23561 - std.typecons.Unique!struct does not destroy struct instance https://github.com/dlang/phobos/pull/8651