This issue has come up on the D.learn forum recently. The following program terminates with core.exception.InvalidMemoryOperationError. (Tested with dmd 2.061 and 2.060)

string foo()
{
    return "a";
}

class Foo
{
    ~this()
    {
        auto s = foo() ~ "b";
    }
}

void main()
{
    new Foo;
}

My guess is that the destructor is being executed at a point where the runtime is not in a state where allocating memory is allowed.

Has the programmer made a mistake? What operations are safe in a class destructor? (I think the question is valid for the destructors of GC-owned struct objects as well.)

Since the programmer should not be expected to know the implementations of every function, the only safe action is to not call any function in a destructor at all, as that function may be allocating dynamic memory under certain conditions. I hope I am wrong. :) Can we expect the runtime be cooperative until all finalizers are executed?

Ali

Reply via email to