www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19386] New: Destructor not called when constructed inside if

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

          Issue ID: 19386
           Summary: Destructor not called when constructed inside if
                    condition, leading to memory leak
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: jesse.bruce economicmodeling.com

Destructor is not called when an object is constructed inside of an if
statement and opCast!bool returns false.

Reproduction:

struct Thing
{
    this(int* i) { ptr = i; (*ptr)++; }
    ~this() { (*ptr)--; }
    T opCast(T:bool)() { return false; }
    int* ptr;
}

Thing makeThing(int* p)
{
    return Thing(p);
}

void main()
{
    int i;
    {
        if(auto t = makeThing(&i))
        {
            import std.stdio;
            writeln("hello?");
        }
    }
    assert(i == 0);
}


Observed output:

core.exception.AssertError regextest.d(24): Assertion failure

Expected output:

--
Nov 09 2018