I have a global D module which contains some helper functions (namely for 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With