Skip to content

Commit

Permalink
Merge pull request #8951 from RazvanN7/Issue_19393
Browse files Browse the repository at this point in the history
Fix Issue 19393 - Structure dtor isn't called after passed to T[] argument. Memory leaks issue
  • Loading branch information
andralex committed Nov 13, 2018
2 parents 38cb3fd + 56e1d68 commit 0a43247
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/dmd/expressionsem.d
Expand Up @@ -1696,6 +1696,7 @@ private bool functionParameters(const ref Loc loc, Scope* sc,
}
else
a = a.implicitCastTo(sc, tbn);
a = a.addDtorHook(sc);
(*elements)[u] = a;
}
// https://issues.dlang.org/show_bug.cgi?id=14395
Expand Down
37 changes: 37 additions & 0 deletions test/runnable/test19393.d
@@ -0,0 +1,37 @@
string result;

struct S
{
this(this)
{
result ~= "A";
}

~this()
{
result ~= "B";
}
}

void foo(const(S)[] ar...)
{
/* postblit gets called on this initialization,
* then when the function returns, the destructor
* gets called => result = "AB";
*/
auto d = ar[0];
}

void bar()
{
/* S(null) needs to be destroyed after the function call,
* that means that another `B` is appended => result = "ABB"
*/
foo(S());
}

void main()
{
bar();
assert(result == "ABB");
}

0 comments on commit 0a43247

Please sign in to comment.