Class deallocator not being called -- bug or oversight?

Jarrett Billingsley kb3ctd2 at yahoo.com
Mon Sep 24 06:48:37 PDT 2007


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. 




More information about the Digitalmars-d-learn mailing list