Bug 14536 – Calling destroy() on a on an extern(C++) class causes a segfault

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2015-05-02T12:50:04Z
Last change time
2018-05-18T03:55:24Z
Keywords
C++, industry
Assigned to
No Owner
Creator
w0rp

Comments

Comment #0 by devw0rp — 2015-05-02T12:50:04Z
class Foo{} extern(C++) class Bar{} void main() { destroy(new Foo()); // Works fine destroy(new Bar()); // segfault! } Calling destroy() on a class marked extern(C++) will segfault. Either it should work and not segfault, or trying to call destroy in this way should result in a compile time error.
Comment #1 by code — 2017-08-10T12:40:32Z
cat > bug.d << CODE extern(C++) class Bar {} void main() { auto inst = new Bar; delete inst; } CODE Crashes in rt_finalize2. Weirdly enough works with `scope` classes. I vaguely remember someone hacked around that crash for scope classes in dmd.
Comment #2 by turkeyman — 2018-04-26T18:24:27Z
Modifying the priority; fixing a segfault is not an enhancement request. destroy() is objectively broken, druntime functions shouldn't crash when given apparently valid inputs used as specified. Also, this has been here for years now, I'd love to bring new attention to it.
Comment #3 by simen.kjaras — 2018-04-27T07:46:52Z
From what I can see, it's caused here: https://github.com/dlang/druntime/blob/master/src/rt/lifetime.d#L1393 c is null, and the first iteration of the do-while loop tries to deref it. Probably, line 1403 (_d_monitordelete) would also fail, since C++ classes don't have the monitor. If it doesn't fail, it's unlikely to do anything nice.
Comment #4 by turkeyman — 2018-05-09T03:29:31Z
Comment #5 by github-bugzilla — 2018-05-18T03:55:23Z
Commits pushed to master at https://github.com/dlang/druntime https://github.com/dlang/druntime/commit/0d98f69de3e4ef83eac86d8c39dce71053fda008 Fix issue 14536 - destroy() works on extern(C++) classes https://github.com/dlang/druntime/commit/ace9f08fd743a3fc1bf6d345d05b0ee4929bfb83 Merge pull request #2176 from TurkeyMan/destroy_cpp_class Fix issue 14536 - destroy() works on extern(C++) classes