Garbage Collection, Allocators/Deallocators and

Ivo Kasiuk i.kasiuk at
Fri Oct 15 09:36:01 PDT 2010

Simen kjaeraas wrote:
> Ivo Kasiuk <i.kasiuk at> wrote:
> > Ok, that makes sense. So the deallocators really should not get called
> > in this case. But why are the destructors not invoked when the GC
> > finalizes the objects?
> For S1 and S2, this is a known bug - destructors of structs on the heap
> don't get called.
> As for C1, I have no idea what's happening. Definitely a bug, though.

Actually it is not a bug but a problem with the implementation:

class C1 {
  ubyte[1_000_000] buf;
  new(size_t size) {
    void* ptr = GC.malloc(size);
    writefln(" = %x", size, ptr);
   return ptr;
  this() { writeln("C1()"); }
  ~this() { writeln("~C1()"); }

GC.malloc is defined as follows:

  static void* malloc(size_t sz, uint ba = 0);

So by calling it without a second argument, I implicitly specified
bitmask 0. The problem with this is that one of these bits is
GC.BlkAttr.FINALIZE. As this is not set, the GC does not finalise the
data contained in the memory block.
The following correction is necessary:

    void* ptr = GC.malloc(size, GC.BlkAttr.FINALIZE);

Then ~C1() gets called as expected.

I think that in theory this should also work for structs. But currently
the same code with a struct instead of a class results in a segfault. I
guess I will have to try again when the struct finalisation bug has been


More information about the Digitalmars-d-learn mailing list