Bug 11952 – struct field initialization with postblit causes un-needed destruction

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-01-19T12:50:00Z
Last change time
2014-05-05T18:43:59Z
Keywords
wrong-code
Assigned to
nobody
Creator
monarchdodra

Comments

Comment #0 by monarchdodra — 2014-01-19T12:50:41Z
Given a struct "B" with a field "sup" of type "A", where "A" has a postblit. Then intialization "sup" triggers a postblit (good), but also destroys the prior value of "sup" (useless). Just the way we can avoid "assign" on first initialization, postblit destruction should be avoided as well. From learn: http://forum.dlang.org/thread/[email protected] In the thread, the user see a wrong amount of reference counts because of this issue.
Comment #1 by monarchdodra — 2014-01-19T12:58:04Z
(In reply to comment #0) > http://forum.dlang.org/thread/[email protected] Actually, I'm bumping to bug. Here is a variant of the code in the thread: //---- import std.stdio; struct B { A sup; this(A a) { writeln("Here"); sup = a; writeln("There"); } } struct A { static int count; this() @disable; //Note this this(int n) { writeln("A.this()"); } this(this) { writeln("A.this(this)"); } ~this() { writeln("A.~this()"); } } void main() { A a = A(1); writeln("Start"); B b = B(a); writeln("End"); } //---- A.this() Start A.this(this) Here A.this(this) A.~this() //WHAT??? There A.~this() End A.~this() A.~this() //---- Here, "A" has a disabled default init. Yet in B's constructor, "A.~this()" is clearly called on a default initialized instance, which should *never* (AFAIK) happen unless previously and explicitly initialized to A.init (not the case here).
Comment #2 by briancschott — 2014-05-05T00:22:15Z
With dmd v2.065 I get this output: A.this() Start A.this(this) Here A.this(this) There A.~this() End A.~this() A.~this() I think this is what is expected, correct?
Comment #3 by k.hara.pg — 2014-05-05T18:43:59Z
(In reply to monarchdodra from comment #0) > Given a struct "B" with a field "sup" of type "A", where "A" has a postblit. > Then intialization "sup" triggers a postblit (good), but also destroys the > prior value of "sup" (useless). This is a dup of issue 9665, and properly fixed from 2.064. *** This issue has been marked as a duplicate of issue 9665 ***