Memory leak with dynamic array
Steven Schveighoffer
schveiguy at yahoo.com
Tue Apr 13 05:04:56 PDT 2010
On Mon, 12 Apr 2010 15:36:19 -0400, Joseph Wakeling
<joseph.wakeling at webdrake.net> wrote:
> Joseph Wakeling wrote:
>> (Actually I'm still having some issues with this, despite using
>> assumeSafeAppend, but more on that in a separate email.)
>
> ... solved; it's interesting to note that assumeSafeAppend has to be
> used in _exactly_ the same scope as the append ~= itself.
>
> e.g. with this loop, the memory blows up:
>
> ////////////////////////////////////////////////////////////////
> foreach(uint i;0..100) {
> x.length = 0;
>
> assumeSafeAppend(x);
>
> foreach(uint j;0..5000)
> foreach(uint k;0..1000)
> x ~= j*k;
>
> writefln("At iteration %u, x has %u elements.",i,x.length);
> }
> ////////////////////////////////////////////////////////////////
This should work as you expect. Can you produce a full code example? Are
you reserving space in x before-hand?
>
> ... while with this one it's OK:
>
> ////////////////////////////////////////////////////////////////
> foreach(uint i;0..100) {
> x.length = 0;
>
> foreach(uint j;0..5000) {
> foreach(uint k;0..1000) {
> assumeSafeAppend(x);
> x ~= j*k;
> }
> }
>
> writefln("At iteration %u, x has %u elements.",i,x.length);
> }
> ////////////////////////////////////////////////////////////////
The first assumeSafeAppend is the only one that matters. Subsequent ones
are wasted cycles. I'm unsure why this one works and the other doesn't.
Again, I need full compilable examples.
-Steve
More information about the Digitalmars-d-learn
mailing list