Ordinarily, it seems legal to append to an array that has been declared at module level (or as a static class member) that hasn't been otherwise initialized, for example:
class Foo {}
private Foo[] cache;
void main() {
auto foo = new Foo();
cache ~= foo;
}
However, when building code like this as a DLL, such as:
class Foo {}
private Foo[] cache;
extern(C) export Foo createFoo() {
auto foo = new Foo();
cache ~= foo;
return foo;
}
and then calling it from another application (in this case, C#), I get core.exception.OutOfMemoryError@src\core\exception.d(647): Memory allocation failed
at the cache ~= foo;
line.
I was able to get around this by adding:
static this() {
cache.length = 0;
}
which seems to fix it, but I'm not entirely sure what's going on, if this is expected behavior, if that's the correct way to handle it, and so on. Does it have something to do with the D runtime being initialized differently in a DLL versus a statically linked program? I am calling Runtime.initialize() as expected when the DLL is attached.