[Issue 15009] New: Object.destroy doesn't call dtors for object in static arrays
    via Digitalmars-d-bugs 
    digitalmars-d-bugs at puremagic.com
       
    Thu Sep  3 11:01:23 PDT 2015
    
    
  
https://issues.dlang.org/show_bug.cgi?id=15009
          Issue ID: 15009
           Summary: Object.destroy doesn't call dtors for object in static
                    arrays
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: druntime
          Assignee: nobody at puremagic.com
          Reporter: nicolas.jinchereau at gmail.com
struct S {
    int x;
    this(int x) { writeln("ctor"); }
    this(this)  { writeln("ctor(postblit)"); }
    ~this()     { writeln("dtor"); }
}
void main(string[] args) {
    S[2]* arr = cast(S[2]*)calloc(1, S.sizeof);
    emplace(arr, S(1));
    destroy(*arr);
    free(arr);
}
output has 5 ctors, and 3 dtors:
  ctor
  ctor(postblit)
  ctor(postblit)
  dtor
  ctor(postblit)
  dtor
  ctor(postblit)
  dtor
fix is to modify this overload of Object.destroy:
void destroy(T : U[n], U, size_t n)(ref T obj)
  if (!is(T == struct))
{
    typeid(T).destroy(&obj);  // +++
    obj[] = U.init;
}
output now has 5 ctors, and 5 dtors, as expected:
  ctor
  ctor(postblit)
  ctor(postblit)
  dtor
  dtor
  dtor
  ctor(postblit)
  dtor
  ctor(postblit)
  dtor
--
    
    
More information about the Digitalmars-d-bugs
mailing list