www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Delete and Auto

While putting together gdc, I found the following delete/finalization 

(1) If a class has a custom deallocator, only the declared destructor 
for that class is called. Inherited destructors are not called.

This patch for internal/gc/gc.d:_d_delclass should fix it:

                 if (c.deallocator)
-                   if (c.destructor)
-                   {
-                       fp_t fp = (fp_t)c.destructor;
-                       (*fp)(*p);              // call destructor
-                   }
+                   _d_callfinalizer(*p);
                     fp_t fp = (fp_t)c.deallocator;
                     (*fp)(*p);                  // call deallocator
-                   *pc = null;                 // zero vptr
                     *p = null;

(2) Deletion of structs

In DMD 0.86, deleting a struct calls the rtl function _d_delmemory. 
_d_delmemory takes a pointer to the memory block to be freed, but the 
dmd compiler is generating code that passes the address of the pointer 
variable.  Which is correct?

(3) Auto classes that do not have a declared destructor, but do have an 
inherited destructor are not finalized when they go out of scope.  They 
are still finalized when then memory is freed:

import std.c.stdio;
class A {
     this() { printf("A ctor\n"); }
     ~this() { printf("A dtor\n"); }
class B : A { }

void test() {  auto B b = new B; }
int main() {
     return 0;

-> test...
-> A ctor
-> ...test
-> A dtor

(4) Object monitors are never freed.

(5) If a struct has a custom allocator, it is called when using 'new', 
but the custom deallocator is not called when using 'delete'.

Apr 30 2004