www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6998] New: std.container.Array destroys class instances

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6998

           Summary: std.container.Array destroys class instances
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: mailme+d nilsb.dyndns.org



---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 23 2011
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6998




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;
}
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 21 2012