Bug 19317 – dip1008 doesn't call the throwable's destructor in _d_delThrowable

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2018-10-19T23:38:45Z
Last change time
2019-03-26T04:30:35Z
Assigned to
No Owner
Creator
Atila Neves
See also
https://issues.dlang.org/show_bug.cgi?id=19463

Comments

Comment #0 by atila.neves — 2018-10-19T23:38:45Z
The last assertion in the code below fails. This makes 1008 far less useful than it could be. Exceptions have error messages, and the only way to construct a useful message with runtime information is to allocate. Given that the purpose of dip1008 is to avoid the GC and still be able to use exceptions, the non error-prone way of doing this is to use an RAII string class. However, that means that _d_delThrowable should call the class's destructor, which it doesn't right now, resulting in a either a leak or the programmer having to manually dispose of the memory. --------------- class MyException: Exception { static int numInstances; this(string msg) { super(msg); ++numInstances; } ~this() { --numInstances; } } void main() { assert(MyException.numInstances == 0); try throw new MyException("oops"); catch(MyException _) assert(MyException.numInstances == 1); assert(MyException.numInstances == 0); } --------------- % dmd -dip1008 -run exception.d [email protected](21): Assertion failure
Comment #1 by iamthewilsonator — 2018-10-20T03:51:03Z
_d_delThrowable does call it https://github.com/dlang/druntime/blob/9a8edfb48e4842180c706ee26ebd8edb10be53f4/src/rt/ehalloc.d#L114 so this must be some compiler buggery that just free's it directly.
Comment #2 by iamthewilsonator — 2018-10-20T15:58:52Z
Hmm, the actual problem seems to be that _d_delThrowable takes a Throwable [1] (N.B: not the most derived type) and after checking its not null, is ref counted, and `GC.removeRange(t);`ing if necessary, proceeds to call rt_finalize on it[2], which recursively calls the destructors of t and is parent classes (i.e. Throwable and Object) but misses any derived destructors. [1]: https://github.com/dlang/druntime/blob/master/src/rt/ehalloc.d#L76 [2]: https://github.com/dlang/druntime/blob/master/src/rt/ehalloc.d#L114
Comment #3 by bugzilla — 2019-03-24T07:14:20Z
Comment #4 by bugzilla — 2019-03-26T04:30:35Z
*** This issue has been marked as a duplicate of issue 19463 ***