Memory leak with dynamic array

Joseph Wakeling joseph.wakeling at webdrake.net
Mon Apr 12 12:36:19 PDT 2010


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);
    }
    ////////////////////////////////////////////////////////////////

... 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);
    }
    ////////////////////////////////////////////////////////////////

Curious question: how come with a registered email address my messages
have to be moderated, whereas via the http interface I can post straight
away? :-P

Best wishes,

    -- Joe


More information about the Digitalmars-d-learn mailing list