Class deallocator not being called -- bug or oversight?

Sean Kelly sean at f4.ca
Mon Sep 24 08:49:20 PDT 2007


Jarrett Billingsley wrote:
> This code:
> 
> import tango.stdc.stdlib: cmalloc = malloc, cfree = free;
> 
> class A
> {
>     new(size_t sz)
>     {
>         Stdout.formatln("A new.");
>         void* p = cmalloc(sz);
> 
>         if(p is null)
>             throw new OutOfMemoryException(__FILE__, __LINE__);
> 
>         return p;
>     }
> 
>     delete(void* p)
>     {
>         Stdout.formatln("A delete.");
> 
>         if(p is null)
>             return;
> 
>         cfree(p);
>     }
> 
>     this()
>     {
>         Stdout.formatln("A ctor.");
>     }
> 
>     ~this()
>     {
>         Stdout.formatln("A dtor.");
>     }
> }
> 
> void main()
> {
>     A a = new A();
>     delete a;
> }
> 
> When compiled with at least DMD 1.018 (also happens with 1.021) and run, 
> gives the output:
> 
> A new.
> A ctor.
> A dtor.
> 
> Where's the "A delete."?  There is none.  I've looked in the spec to see if 
> I'm doing it right, and it sure looks like it.
> 
> If A is made a struct (and the ctor and dtor are removed), I get:
> 
> A new.
> A delete.
> 
> As expected.
> 
> Something weird is going on. 

You're right.  This was a bug in the Tango runtime.  It's now fixed in 
the Tango trunk for both DMD and GDC.

Oh for what it's worth, I can't figure out where Phobos is calling class 
deallocators--the block I'd expect to be used in internal/gc/gc.d is 
commented out.  I don't suppose someone could test this with Phobos to 
see if it works?


Sean


More information about the Digitalmars-d-learn mailing list