Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling module-level functions from destructor in D (seems to throw an OutOfMemoryError)

Tags:

d

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"); }
}
like image 985
Mark LeMoine Avatar asked Sep 06 '11 16:09

Mark LeMoine


1 Answers

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.

like image 113
Vladimir Panteleev Avatar answered Oct 15 '22 11:10

Vladimir Panteleev