[Issue 19386] Destructor not called when constructed inside if condition, leading to memory leak

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Nov 10 00:38:16 UTC 2018


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

--- Comment #1 from Jesse Bruce <jesse.bruce at economicmodeling.com> ---
Moving object creation outside of the if statement works as intended.

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;
    {
        auto t = makeThing(&i);
        if(t)
        {
            import std.stdio;
            writeln("hello?");
        }
    }
    assert(i == 0);
}

Observed output:

--


More information about the Digitalmars-d-bugs mailing list