You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[REG 2.103.0] GC never finalizes a class whose only destructors are field destructors #23979
A class with no ~this() (or any base class) whose fields have destructors is allocated without BlkAttr.FINALIZE, so the GC never runs the field destructors, either on collection or at program exit. destroy and scope work correctly. Adding an empty ~this() {} to the class works around it.
importcore.memory : GC;
__gsharedint finalized;
structR {
int x;
~this() {
if (x) ++finalized;
}
}
// no ~this(): only a field destructorclassC {
R r = R(1);
}
voidmain() {
assert(GC.getAttr(cast(void*) new C) & GC.BlkAttr.FINALIZE); // failsforeach (i; 0..100) new C;
GC.collect();
assert(finalized >0); // also fails: no C is ever finalized (comment out the first assert)
}
Fails with DMD64 D Compiler v2.113.0 and GDC 14.2 (frontend 2.108). Passes with LDC 1.43.0, which lowers new to its own _d_allocclass(ClassInfo).
Cause:core.lifetime._d_newclassT sets FINALIZE only if __traits(hasMember, T, "__dtor"). __dtor exists only for a user-declared destructor; a class whose destruction comes only from its fields has just __xdtor. typeid(C).destructor is set and ClassFlags.hasDtor is set, so the ClassInfo is correct.
Regression: This started in 2.103.0 with #14837, which turned _d_newclass into the _d_newclassT template. The old function checked ClassFlags.hasDtor, which includes field destructors; the template only checks for __dtor. I confirmed this by building both releases from their tags: 2.102.2 finalizes the class and 2.103.0 does not. GDC shows the same split between GDC 12 and GDC 13.
Activity
Fix dlang#23979 - GC never finalizes a class whose only destructors a…
Fix dlang#23979: GC never finalizes a class whose only destructors ar…
Fix #23979: GC never finalizes a class whose only destructors are fie…