Mark LeMoine
Mark LeMoine

Reputation: 4628

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 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"); }
}

Upvotes: 6

Views: 137

Answers (1)

Vladimir Panteleev
Vladimir Panteleev

Reputation: 25187

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.

Upvotes: 4

Related Questions