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: [email protected]
Reporter: [email protected]
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:
[email protected](24): Assertion failure
Expected output:
--