Calling module-level functions from destructor in D (seems to throw an OutOfMemoryError)
I have a global D module which contains some helper functions (namely fo开发者_JS百科r logging), which are at module-level and not in a class. However, when calling these functions from a destructor, I get a core.exception.OutOfMemoryError and/or the app hangs and crashes. Am I doing something wrong here?
A stripped-down test case:
logger.d
module main.logger;
void log(const(char)[] msg) {
auto time = // GET TIME OF DAY SOMEHOW
std.stdio.writeln(std.conv.to!string(time) ~ " " ~ msg);
}
class.d
module main.class;
import main.logger;
class A {
public:
this() {}
~this() { log("Destructor"); }
}
The garbage collector currently does not support thrown exceptions or memory allocations called from within a finalizer. Thus, you can't reliably do anything which causes an allocation or throws an uncaught exception from inside a class destructor.
精彩评论