Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

lifetime of dynamically allocated memory

Moderators: larsivi kris

Posted: 05/31/10 20:22:25

I'm trying to figure out if a dynamically allocated memory in D is getting collected with this simple test:

class Foo {

~this() {

// destructor

}

}

struct Item {

Foo a;

}

Item[] items; items.length = 1; items[0] = Item(); items[0].a = new Foo();

items = null;

GC.collect();

I assume that calling GC.collect(); would go through and free up memory for any unreferenced objects. It seems that the destructor for Foo isn't being called. I'm not sure if the reference is dangling somewhere or do I have to explicitly call 'delete items[0].a' to ensure that the memory is freed?

Author Message

Posted: 05/31/10 21:39:39

the D GC doesn't guarantee all collectible allocations are processed on a GC.collect() call. It may, for example, take a few iterations before things are collected fully. The other issue is that D dtors are actually finalizers in disguise. This means there's no guarantee they will ever be invoked