www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14078] New: Call DTor for stack allocated objects

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 puremagic.com
          Reporter: rswhite4 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.

--
Jan 29 2015