OutOfMemoryError in D DLL appending to module-level array
cc
cc at nevernet.com
Sun May 2 02:34:41 UTC 2021
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:
```d
class Foo {}
private Foo[] cache;
void main() {
auto foo = new Foo();
cache ~= foo;
}
```
However, when building code like this as a DLL, such as:
```d
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 at src\core\exception.d(647):
Memory allocation failed` at the `cache ~= foo;` line.
I was able to get around this by adding:
```d
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.
More information about the Digitalmars-d-learn
mailing list