Issue 23561 - std.typecons.Unique!struct does not destroy struct instance
Summary: std.typecons.Unique!struct does not destroy struct instance
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Windows
: P3 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2022-12-15 19:29 UTC by Nick Treleaven
Modified: 2022-12-24 12:41 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Nick Treleaven 2022-12-15 19:29:46 UTC
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 Vital 2022-12-16 04:48:04 UTC
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 Vital 2022-12-16 04:48:59 UTC
But works when S is class:

class S
{
    ~this() { 
    	i++; 
    	writeln("DTOR: i == ", i ); 
    }
}
Comment 3 Dlang Bot 2022-12-16 13:17:00 UTC
@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 Dlang Bot 2022-12-24 12:41:00 UTC
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