On Saturday, 20 September 2014 at 22:46:10 UTC, John Colvin wrote:
import core.stdc.stdio;
struct S
{
~this()
{
printf("%x\n".ptr, &this);
}
}
void main()
{
S* sp = new S;
destroy(*sp);
S s;
destroy(s);
auto sa = new S[2];
foreach(ref s_; sa)
destroy(s_);
}
output:
4002dff0
bfa89a70
4002dfe0
4002dfe1
bfa89a70
Note the double destruction of s
Its seems that calling destroy on a stack-allocated struct is a
no-no (unless you have a re-entrant destructor). The other two
examples seem OK though.
Am I in dangerous territory? Will I see unexpected
double-destructions in some cases?
Destroy leaves your struct in its T.init state, and T.init should
always be destroyable. So even though the destroyer gets called
twice, you should be perfectly safe.
FYI, "move" relies on this mechanism: it sets object state to
T.init, to make sure the later stack destruction has no side
effect.