Problems with GC, trees and array concatenation

Babele Dunnit babele.dunnit at gmail.com
Mon Jun 4 07:43:45 PDT 2007


Frits van Bommel Wrote:


> Looking at the GC code I can't seem to find any place where arr[length 
> .. _gc.cap(arr)] (the unused part of the array allocation) is 
> initialized. This could explain the issue if your arrays have different 
> lengths (since the data from an longer old array may be present after a 
> shorter new array, and is considered as "live" pointers by the GC 
> because it's within the same allocation block).
> 
> However, this seems to be the case for straight allocation as well, not 
> just concatenation.
> If this is the cause, you probably have the same issue if you replace
>      indi[] = testPop1.individuals ~ testPop2.individuals;
> by
>      auto tmp = new Individual[](testPop1.length + testPop2.length);
>      tmp[0 .. testPop1.length] = testPop1;
>      tmp[testPop1.length .. $] = testPop2;
>      indi[] = tmp;
> Is this the case?


Woagh!

You got it, man! Actually, the Population is not an array itself but a container, so the correct code is:

        auto tmp = new Individual[](testPop1.individuals.length + testPop2.individuals.length);
             tmp[0 .. testPop1.individuals.length] = testPop1.individuals;
             tmp[testPop1.individuals.length .. $] = testPop2.individuals;
             indi[] = tmp;

but the leak is there!

I still do not understand one thing, though: you say "if your arrays have different lengths", but my arrays are fixed in size... they are even static! Shouldn't  GC try to allocate and recycle always the same fixed-size chunk of memory?

and why the leak does not shows up under OsX??

anyway, thx!

Babele




More information about the Digitalmars-d mailing list