Bug? GC collects memory that references itself.

Jeremie Pelletier jeremiep at gmail.com
Mon Oct 26 19:45:13 PDT 2009


I need objects that may live without any references in GC memory, this 
is for bindings to a C++ library. Using ranges or roots would be very 
inneficient so my solution was to have the objects reference themselves 
until the C++ side of the object calls a finalizer which nullify the 
reference to let the GC collect the memory.

The GC doesn't see things this way however, and collects the objects 
even when they reference themselves.

I've made a simple test program, the objects should never get collected.

---

import core.memory;
import std.stdio;

class Foo {
	Foo self;
	this() { self = this; }
	~this() { assert(!self); }
}

void main() {
	foreach(i; 0 .. 50) new Foo;
	GC.collect();
	writeln("No object collected!");
}

---

If its a feature of the GC to prevent objects from never being 
collected, how do I bypass it?

Jeremie


More information about the Digitalmars-d-learn mailing list