Bug 2306 – Scope for dynamic arrays should free memory.

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P3
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2008-08-22T22:15:05Z
Last change time
2019-08-20T11:34:00Z
Assigned to
No Owner
Creator
David Simcha

Comments

Comment #0 by dsimcha — 2008-08-22T22:15:05Z
Because of the overhead of frequent garbage collections, as well as false pointer issues with conservative GC, it is sometimes desirable to use RAII-style memory management for large arrays. Most RAII stuff in D is done using the scope keyword. However, when applied to dynamic arrays, the scope keyword apparently does absolutely nothing. The following program runs out of memory after 3 iterations due to false pointer issues, as is expected when allocating a 400 MB array in 4 GB address space w/ conservative GC as the only method being used to free memory. import std.stdio; void main() { uint count = 0; while(true) { test(); writefln(++count); } } void test() { scope uint[] foo = new uint[100_000_000]; } However, the following actually runs indefinitely: import std.stdio; void main() { uint count = 0; while(true) { test(); writefln(++count); } } void test() { uint[] foo = new uint[100_000_000]; scope(exit) delete foo; } This isn't a very serious bug, since there's an obvious, simple workaround, but it's still an inconsistency in the language design, and ideally should be fixed.
Comment #1 by 2korden — 2008-08-24T12:19:30Z
In fact, it shouldn't allocate in the first place (in my opinion). C99 is able to stack-allocate arrays and so should D: void test(int size) { int[size] stackAllocatedStaticArray; scope int[] stackAllocatedDynamicArray = new int[size]; //exactly the same as the following: // int[size] tmp; // int[] stackAllocatedDynamicArray = tmp[0..$]; }
Comment #2 by dsimcha — 2008-10-10T21:11:39Z
I'd disagree that scope dynamic arrays should be stack allocated. The int[variableSize] syntax for that is perfectly good, if Walter wants to implement it. If you're allocating a very large array, you probably don't want to allocate it on the stack so you don't overflow the stack. Furthermore, you can't resize stack allocated arrays after they're created, at least not in C99.
Comment #3 by dsimcha — 2008-11-17T21:36:15Z
For a possible resolution to this, see http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=79672. It's a post by Andrei describing a second stack on which objects like this could be allocated. If such a second stack could be built into the runtime, all scope objects could be allocated there.
Comment #4 by 2korden — 2008-11-19T20:07:50Z
(In reply to comment #2) > I'd disagree that scope dynamic arrays should be stack allocated. The > int[variableSize] syntax for that is perfectly good, if Walter wants to > implement it. If you're allocating a very large array, you probably don't want > to allocate it on the stack so you don't overflow the stack. I take my words back. I believe now it is up to the compiler to decide where to put that scope array. > Furthermore, you can't resize stack allocated arrays after they're created, at least not in C99. Well, yes and no. You can't resize C99-like variable-length array because it is supposed to behave like a static one. But scope T[] array may be easily resized even if it is stack allocated and initially points to stack. It could cause some problems if array data would be realloc'ated upon resize (you may end up with trying to reallocate stack space), but since it doesn't and old array is left for garage collector recycling, your data will be moved to heap after firrst array resize while a copy of them stays on stack until you leave the scope: P.S. Now that I wrote the last sentence I start wondering if it really behaves as I think :) Is the array resize policy documented anywhere?
Comment #5 by dfj1esp02 — 2008-12-11T06:25:40Z
(In reply to comment #0) > void test() { > scope uint[] foo = new uint[100_000_000]; > } > void test() { > uint[] foo = new uint[100_000_000]; > scope(exit) delete foo; > } > > This isn't a very serious bug, since there's an obvious, simple workaround, but > it's still an inconsistency in the language design, and ideally should be > fixed. > consider this ----- void test() { scope uint[] foo = new uint[100_000_000]; scope uint[] foo1 = foo; } ----- how much should it free?
Comment #6 by dsimcha — 2008-12-11T22:54:29Z
(In reply to comment #5) > > consider this > ----- > void test() { > scope uint[] foo = new uint[100_000_000]; > scope uint[] foo1 = foo; > } > ----- > how much should it free? > I guess this is where the T[new] and T[] types that have been proposed come in. foo would be T[new] so it would determine the freeing stuff. foo1 would be T[], so it wouldn't.
Comment #7 by dsimcha — 2010-08-11T14:16:06Z
I'm closing this since we're doing away with scope classes and moving them to a library solution. The corresponding library solution for arrays is std.container.Array.
Comment #8 by nfxjfg — 2010-08-11T14:25:52Z
Still valid for D1.
Comment #9 by bearophile_hugs — 2010-08-11T14:52:54Z
I think D1 is feature-frozen, I don't think this will be ever added by Walter.
Comment #10 by leandro.lucarella — 2010-08-11T15:13:28Z
(In reply to comment #9) > I think D1 is feature-frozen, I don't think this will be ever added by Walter. But this is not a feature request, is a bug report.
Comment #11 by nfxjfg — 2010-08-11T15:21:37Z
Nobody knows whether this is a bug report or an enhancement request because D1 is too underspecified. Walter has added features to D1 in the past, although I suspect he's keeping that low to advertise D2.
Comment #12 by razvan.nitu1305 — 2019-08-20T11:34:00Z
D1 is not maintained anymore and now we have a library solution for scope classes in std.typecons.scoped.