lifetime of dynamically allocated memory

torhu no at spam.invalid
Mon May 31 14:18:00 PDT 2010


On 31.05.2010 22:27, dave wrote:
> I'm trying to figure out if a dynamically allocated memory in D is getting
> collected with this simple test (using tango):
>
> 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(); forces gc to 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 still used somewhere or do I have to
> explicitly call 'delete items[0].a' to ensure that the memory is freed?

I'm not sure why it doesn't work.  But it's always better to post the 
code you're actually using, since now we can't be sure that you're not 
doing something else then what you think you're doing.  If you know what 
I mean. :)


More information about the Digitalmars-d-learn mailing list