Bug 18995 – std.array.array doesn't free elements

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-06-15T17:28:49Z
Last change time
2018-06-21T15:43:02Z
Keywords
pull
Assigned to
Steven Schveighoffer
Creator
Andrea Fontana

Comments

Comment #0 by trikkuz — 2018-06-15T17:28:49Z
see: https://forum.dlang.org/post/[email protected] 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"); }
Comment #1 by schveiguy — 2018-06-15T18:10:59Z
The issue is with array using uninitializedArray to allocate. This function does not (and really cannot) initialize the proper GC bits to call the destructors properly. std.array.array should use normal array allocation functions when the type being allocated has a destructor.
Comment #2 by schveiguy — 2018-06-15T20:40:56Z
Comment #3 by github-bugzilla — 2018-06-20T06:58:41Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/4fe18b905526b4c1d0fb19aa28ccfcddf27ffcdc Fix issue 18995 - Make sure GC calls destructor when using std.array.array https://github.com/dlang/phobos/commit/db66b82b178480a636024371d76d016e6ee03971 Merge pull request #6584 from schveiguy/fix18995 Fix issue 18995 - Make sure GC calls destructor when using std.array.array merged-on-behalf-of: Petar Kirov <[email protected]>
Comment #4 by schveiguy — 2018-06-21T15:43:02Z
*** Issue 12444 has been marked as a duplicate of this issue. ***