[Issue 14078] New: Call DTor for stack allocated objects

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Jan 29 11:05:40 PST 2015


https://issues.dlang.org/show_bug.cgi?id=14078

          Issue ID: 14078
           Summary: Call DTor for stack allocated objects
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: DMD
          Assignee: nobody at puremagic.com
          Reporter: rswhite4 at gmail.com

----
import std.stdio;

class A {
    int id = 0;

    this(int the_id) {
        this.id = the_id;

        writeln("CTor for #", this.id);
    }

    ~this() {
        writeln("DTor for #", this.id);
    }
}

void main() {
    scope A a1 = new A(1);

    import std.conv : emplace;

    void[__traits(classInstanceSize, A)] buf = void;
    A a2 = emplace!(A)(buf, 2);
}
----

Current output:
CTor for #1
CTor for #2
DTor for #1

Desirable output:
CTor for #1
CTor for #2
DTor for #2
DTor for #1


It would be nice if D would recognize that a2 is stack allocated and treat it
like a struct, so that the DTor is called at the end of the scope.

--


More information about the Digitalmars-d-bugs mailing list