Bug 14246 – RAII - proper destruction of partially constructed objects/structs

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-03-04T19:23:55Z
Last change time
2018-09-28T21:05:07Z
Keywords
industry, preapproved, safe, spec, wrong-code
Assigned to
No Owner
Creator
Marc Schütz

Comments

Comment #0 by schuetzm — 2015-03-04T19:23:55Z
Currently, when a constructor throws, it will not call destructors of already initialized member fields. The user is responsible for catching exceptions or using `scope(failure)` and cleaning up manually. Of course, this is error prone and can easily be forgotten. I suggest to apply the same heuristic to mutable fields that is also use for tracking the initialization of immutable fields: The first "assignment" to a field must not appear in a loop or after a label, and calls the constructor instead of opAssign(). For immutable and const fields, no further writes are allowed; mutable fields can be changed as desired. At each point where an exception can occur, the compiler is then aware which fields have already been initialized, and can call their destructor.
Comment #1 by acehreli — 2015-03-04T22:51:32Z
Comment #2 by petar.p.kirov — 2016-08-14T18:19:48Z
*** Issue 16388 has been marked as a duplicate of this issue. ***
Comment #3 by dfj1esp02 — 2016-08-16T13:23:56Z
How does it affect safety?
Comment #4 by andrei — 2017-05-19T19:56:20Z
Comment #5 by bugzilla — 2017-05-19T22:37:23Z
Comment #6 by bugzilla — 2017-05-19T22:45:42Z
Shachar Shemesh wrote this example: import std.stdio; struct A { int a = 3; this( int var ) { a += var; } ~this() { writeln("A down ", a); } } struct B { A a; this( int var ) { a = A(var+1); throw new Exception("An exception"); } } void main() { try { auto b = B(2); } catch( Exception ex ) { } } I'd expect A's destructor to run, which does not seem to be the case.
Comment #7 by bugzilla — 2017-05-20T07:37:23Z
Comment #8 by github-bugzilla — 2017-06-11T05:26:00Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/1e755c648e5142719d50dcf984f10ace5b8f8357 fix Issue 14246 - RAII - proper destruction of partially constructed objects/structs https://github.com/dlang/dmd/commit/6b3d406be3670613d862ec6eda1bf3669443a926 Merge pull request #6816 from WalterBright/fix14246 fix Issue 14246 - RAII - proper destruction of partially constructed … merged-on-behalf-of: Andrei Alexandrescu <[email protected]>
Comment #9 by mm — 2017-07-30T10:17:16Z
The fix for this has been reverted in [1] and testing Shachar Shemesh's example with 2.075.0 shows that the destructor for A is not run. That this issue has been fixed is also erroneously shown in the changelog for 2.075.0. [1] https://github.com/dlang/dmd/pull/6913
Comment #10 by github-bugzilla — 2017-08-07T13:16:40Z
Commits pushed to newCTFE at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/1e755c648e5142719d50dcf984f10ace5b8f8357 fix Issue 14246 - RAII - proper destruction of partially constructed objects/structs https://github.com/dlang/dmd/commit/6b3d406be3670613d862ec6eda1bf3669443a926 Merge pull request #6816 from WalterBright/fix14246
Comment #11 by github-bugzilla — 2017-08-18T18:11:48Z
Commit pushed to stable at https://github.com/dlang/dlang.org https://github.com/dlang/dlang.org/commit/45ca5e35d3de824e104c3049083eb23fa03775c5 Remove wrong changelog entry The fix for issue 14246 was reverted, it is not included in 2.075.0 (see bugzilla page for details), and the issue has been reopened.
Comment #12 by github-bugzilla — 2017-08-22T18:07:40Z
Comment #13 by bugzilla — 2018-09-14T22:34:58Z
Comment #14 by github-bugzilla — 2018-09-28T21:05:07Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/a9ee4ce6c0e4e0e4981a367470e8645f2cb85271 fix Issue 14246 - RAII - proper destruction of partially constructed objects/structs https://github.com/dlang/dmd/commit/1933ecd62808b95756351e746848033771dce825 Merge pull request #8697 from WalterBright/reboot14246 fix Issue 14246 - RAII - proper destruction of partially constructed …