www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18995] New: std.array.array doesn't free elements

https://issues.dlang.org/show_bug.cgi?id=18995

          Issue ID: 18995
           Summary: std.array.array doesn't free elements
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: trikkuz gmail.com

see: https://forum.dlang.org/post/ypotpzhjwrqohljubizt forum.dlang.org

An example:

import std.experimental.all;

auto assumePure(T)(T t)
if (isFunctionPointer!T || isDelegate!T)
{
    enum attrs = functionAttributes!T | FunctionAttribute.pure_;
    return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
}

struct A
{       

     disable this();
    this(size_t i) { ctors++; this.i = i; writeln("CTOR ", i); }
    ~this() { dtors++; writeln("DTOR ", i); }

    void dbg() { ctors++;  writeln("POSTBLIT ", i); }
    pure this(this) 
    { 
        assumePure(&dbg)(); 
    }

    static size_t ctors = 0;
    static size_t dtors = 0;
    size_t i;
}


struct Range
{
    // property size_t length = 4;  // Uncomment this line for a similar
result!
     property A front() { return A(count); }
    void popFront() { count++; }
     property empty() { return count > 3; }
    size_t count = 0;
}

void main()
{
    // This is never freed, apparently
    {
        auto tmp = Range();
                tmp.array.writeln;
    }

    import core.memory;
    GC.collect();
}

static ~this() 
{ 
    writeln(A.ctors, "ctors vs ", A.dtors, " dtors"); 
}

--
Jun 15 2018