[Issue 19119] App crashes with strange behavior of ctors/dtors from CPP

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jul 27 17:42:22 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=19119

--- Comment #8 from Manu <turkeyman at gmail.com> ---
(In reply to Vladimir Marchevsky from comment #7)
> Sounds logical. I've tried to additionally export Bar, map it in D and also
> to add Foo::a and Foo::b to mapping to make it full. Problem has gone and it
> seems to work correct without crashes or memory leaks.

Yeah, sorry, I didn't notice that the classes were not matching!
If you're going to allocate instances of a class, they need to be the same
size.

> But how does it happen to work under some conditions before (like with
> non-virtual dtor)? Does "virtual" change used methods to create/delete
> objects somehow or was that sort of random luck?..

You were lucky. The CPP ctor/dtor were assigning and destroying a,b which were
outside of the memory allocation (since you didn't define them in the class
that D allocated.
It's likely that there's a minimum allocation granularity (like 16 bytes or
something) and those members just happened to fit inside... but when you added
the vtable, the class got bigger, maybe b was outside of the minimum allocation
block... or lots of possibilities. You were writing to unallocated memory;
anything could happen!

--


More information about the Digitalmars-d-bugs mailing list