Struct destructors not always called?
Jeremy DeHaan via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Dec 27 10:40:55 PST 2015
I was playing around with some code today and I noticed that in
some cases struct destructors are not called.
for example:
impost std.stdio;
SomeStruct global;
void main()
{
SomeStruct inMain;
writeln(global.thing);
writeln(inMain.thing);
writeln(getSomeInt());
}
int getSomeInt()
{
static SomeStruct inner;
return inner.thing;
}
struct SomeStruct
{
int thing = 100;
~this()
{
writeln("destructor");
}
output is
100
100
100
destructor
Only inMain's destructor is ever called, or at least it is the
only one that ever prints "destructor" to the console. Are there
special rules for structs that I'm unaware of?
More information about the Digitalmars-d-learn
mailing list