Skip to content

Commit 1387ae2

Browse files
authored
Merge pull request #97 from n8sh/dynamicarray-dont-destroy-object
Fix issue #96 - DynamicArray should not call destructors for objects merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
2 parents 387f794 + c81116f commit 1387ae2

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/containers/dynamicarray.d

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ struct DynamicArray(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange
5252

5353
if (arr is null)
5454
return;
55-
foreach (ref item; arr[0 .. l])
55+
56+
static if ((is(T == struct) || is(T == union))
57+
&& __traits(hasMember, T, "__xdtor"))
5658
{
57-
static if (is(T == class) || is(T == interface))
58-
destroy(item);
59-
else static if (is(T == struct) || is(T == union))
60-
static if (__traits(hasMember, T, "__xdtor"))
61-
item.__xdtor();
59+
foreach (ref item; arr[0 .. l])
60+
{
61+
item.__xdtor();
62+
}
6263
}
6364
static if (useGC)
6465
{
@@ -218,11 +219,6 @@ struct DynamicArray(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange
218219
{
219220
if (i < this.l)
220221
{
221-
static if (is(T == class) || is(T == interface))
222-
destroy(arr[i]);
223-
else
224-
typeid(T).destroy(&arr[i]);
225-
226222
auto next = i + 1;
227223
while (next < this.l)
228224
{
@@ -231,6 +227,11 @@ struct DynamicArray(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange
231227
}
232228

233229
--l;
230+
static if ((is(T == struct) || is(T == union))
231+
&& __traits(hasMember, T, "__xdtor"))
232+
{
233+
arr[l].__xdtor();
234+
}
234235
}
235236
else
236237
{
@@ -365,7 +366,7 @@ unittest
365366
DynamicArray!(Cls) arr;
366367
arr.insert(new Cls( & a));
367368
}
368-
assert(a == 1);
369+
assert(a == 0); // Destructor not called.
369370
}
370371

371372
unittest
@@ -417,7 +418,7 @@ unittest
417418
arr.insert(new Cls(&a));
418419

419420
arr.remove(0);
420-
assert(a == 1);
421+
assert(a == 0); // Destructor not called.
421422
}
422423

423424
unittest

0 commit comments

Comments
 (0)