digitalmars.D.learn - Why is a static struct's dtor called at the exit of a function?
- Andrej Mitrovic (41/41) Jul 20 2011 import std.stdio;
import std.stdio; void main() { writeln("test"); test(); writeln(); writeln("test"); test(); } struct Foo { int state; this (int i) { state = i; } ~this() { writeln("dtor"); } } void test() { static Foo foo = Foo(1); writeln(foo.state); } Prints: test 1 dtor test 1 dtor Shouldn't static variables be left untouched? I tried to use a struct to handle an external device context, however the dtor would be kept called between function calls, so this caused crashes for me. Using a class instead fixes my issue of course. I thought "static" for variables implied it has lifetime until the end of the application, regardless of whether the memory for the variable was allocated on the heap or is in the .data segment or somewhere else.
Jul 20 2011