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

2019-05-21 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=19386

Dlang Bot  changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 Resolution|--- |FIXED

--- Comment #2 from Dlang Bot  ---
dlang/dmd pull request #9830 "Fix issue 19386 - Destructor not called when
constructed inside if condition" was merged into stable:

- 9d4f118670ef68416a0d13108e3cdcf13271 by سليمان السهمي  (Suleyman Sahmi):
  Fix issue 19386 - Destructor not called when constructed inside if condition
  This happens when the if condition evaluates to false at runtime

https://github.com/dlang/dmd/pull/9830

--


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

2018-11-09 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=19386

--- Comment #1 from Jesse Bruce  ---
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();
if(t)
{
import std.stdio;
writeln("hello?");
}
}
assert(i == 0);
}

Observed output:

--