Bug 6998 – std.container.Array destroys class instances
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-11-23T16:40:00Z
Last change time
2014-04-12T11:54:46Z
Assigned to
nobody
Creator
nilsbossung
Comments
Comment #0 by nilsbossung — 2011-11-23T16:40:00Z
---
class C {
int i;
}
auto c = new C;
c.i = 42;
Array!C a;
a ~= c;
a.clear;
assert(c.i == 42); // fails
---
Tested with dmd 2.056.
Problem does not arise with structs.
Comment #1 by nilsbossung — 2012-01-21T01:34:19Z
This happens:
Array!T.Payload's destructor does
---
foreach (ref e; _payload) .clear(e);
---
where .clear is object.clear:
---
void clear(T)(T obj) if (is(T == class))
{
rt_finalize(cast(void*)obj);
}
void clear(T)(ref T obj)
if (!is(T == struct) && !is(T == class) && !_isStaticArray!T)
{
obj = T.init;
}
---
(and other overloads that are not of interest)
That is, when object.clear is given a class instance reference, it destroys the
object, and when given a pointer (e.g. to a 'new'ed struct instance),
it nulls the pointer, but doesn't touch the pointer's target.
So maybe Array shouldn't call object.clear, or it should check for
!is(T == class). Or maybe object.clear shouldn't destroy class instances. To me
this would look more in line with the other versions of object.clear:
---
void clear(T)(ref T obj) if (is(T == class))
{
obj = T.init;
}
---