Bug 14708 – destructor for temporary not called during stack unwinding

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2015-06-17T15:49:07Z
Last change time
2020-09-09T22:25:01Z
Keywords
backend, pull, wrong-code
Assigned to
No Owner
Creator
Kenji Hara
See also
https://issues.dlang.org/show_bug.cgi?id=14903

Comments

Comment #0 by k.hara.pg — 2015-06-17T15:49:07Z
Reduced test case: extern(C) int printf(const char*, ...); bool dtor = false; struct S { int n; void* get(void* p = null) { return null; } ~this() { printf("dtor\n"); dtor = true; } } S makeS(int n) { return S(n); } void foo(void* x) { throw new Exception("fail!"); } void test(int len = 2) { foo(makeS(1).get()); // A temporary is allocated on stack for the // return value from makeS(1). // When foo throws exception, it's dtor should be called // during unwinding stack, but it does not happen in Win64. } void main() { try { test(); } catch (Exception e) {} assert(dtor); // fails! } I confirmed the issue on Win64, but I think it would happen in all platforms excepting Win32. Currently dmd handles the destructor calls for temporaries by using OPdctor and OPddtor. It works correctly for Structural Exception Handling (SEH) in Win32. On the other hand, Win64 and other Posix platforms generate exception handler tables and use them from druntime. However dmd generates them only for the explicit try-catch and try-finally statements, and doesn't recognize the implied try-block between OPdctor and OPddtor elements. By that, throwing exception from 'foo' skips dtor call for the temporary in test(). It's an obstacle to my compiler fix for issue 14696. https://github.com/D-Programming-Language/dmd/pull/4749
Comment #1 by ketmar — 2015-06-17T16:45:11Z
GNU/Linux x86 fails too, as expected. p.s. but GDC works. +1 to Iain! ;-)
Comment #2 by github-bugzilla — 2015-09-02T08:56:36Z
Commits pushed to stable at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/e20c63cb28decb66c792b1dfc10d79aa0aebd7cf fix Issue 14708 - destructor for temporary not called during stack unwinding https://github.com/D-Programming-Language/dmd/commit/38d31b0364e555ce42a8f47ca656e59b1cf90aad Merge pull request #5003 from 9rnsr/fix14696_cdmd Issue 14696, 14708, and [REG2.068] 14979 - Fix destructor issues for temporary in conditional operator
Comment #3 by github-bugzilla — 2015-09-07T13:36:59Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/e20c63cb28decb66c792b1dfc10d79aa0aebd7cf fix Issue 14708 - destructor for temporary not called during stack unwinding https://github.com/D-Programming-Language/dmd/commit/38d31b0364e555ce42a8f47ca656e59b1cf90aad Merge pull request #5003 from 9rnsr/fix14696_cdmd
Comment #4 by code — 2015-09-23T09:05:12Z
FWIW dmd currently emits SEH wrappers around any temporary destruction. Weird that nobody yet noticed that. Doesn't this severely impact performance on Win32?
Comment #5 by github-bugzilla — 2015-09-23T11:01:09Z
Commit pushed to stable at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/1a807d16b5d8ba87dbfd4344942a6708ef516896 Revert "fix Issue 14708 - destructor for temporary not called during stack unwinding" This reverts commit e20c63cb28decb66c792b1dfc10d79aa0aebd7cf. # Conflicts: # test/runnable/sdtor.d
Comment #6 by k.hara.pg — 2015-09-23T11:39:00Z
It was once fixed in 2.068.0, but the compiler change was reverted because of issue 15056. Therefore, the issue will be reopened since 2.068.2.
Comment #7 by github-bugzilla — 2015-09-24T08:16:00Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/1a807d16b5d8ba87dbfd4344942a6708ef516896 Revert "fix Issue 14708 - destructor for temporary not called during stack unwinding"
Comment #8 by dlang-bot — 2020-09-03T07:16:09Z
@WalterBright created dlang/dmd pull request #11676 "fix Issue 14708 - destructor for temporary not called during stack un…" fixing this issue: - fix Issue 14708 - destructor for temporary not called during stack unwinding https://github.com/dlang/dmd/pull/11676
Comment #9 by dlang-bot — 2020-09-09T22:25:01Z
dlang/dmd pull request #11676 "fix Issue 14708 - destructor for temporary not called during stack un…" was merged into master: - 206fa12a6754a04d12c9e2ac0e6028c6dfa92b9a by Walter Bright: fix Issue 14708 - destructor for temporary not called during stack unwinding https://github.com/dlang/dmd/pull/11676