digitalmars.D.learn - interfacing c++
- Markus (50/50) Nov 21 2017 hi, im trying to interface a cpp class. I'd like to interface a
hi, im trying to interface a cpp class. I'd like to interface a bigger library and I'm trying to figure out the minimum effort. --- c++ part: #include <iostream> class some_class { public: static some_class* __ctor(); some_class(); ~some_class(); void some_method(); }; some_class* some_class::__ctor() { auto thiz = new some_class(); std::cout << "hello from __ctor, thiz:" << thiz << std::endl; return thiz; } some_class::some_class() { std::cout << "some_class constructor, this:" << this << std::endl; } some_class::~some_class() { std::cout << "some_class destructor, this:" << this << std::endl; } void some_class::some_method() { std::cout << "some_class some_method, this:" << this << std::endl; } --- d part: extern (C++) { class some_class { final this(); final ~this(); final void some_method(); } } void main() { some_class someClass = new some_class(); // works, __ctor() gets called, and it calls the constructor. someClass.some_method; // works destroy(someClass); // crashes (SIGSEGV) inside lifetime.d rt_finalize2() } --- OS: ubuntu 17.10 compiler: DMD64 D Compiler v2.077.0 I could do the instancing/destruction by functions and write a custom d class that calls these methods in this()/~this(). But I was hoping not needing to write a class in D AND in cpp. and i was hoping to save another step/level of instancing. Any idea how to make the destructor of cpp compatible with "~this()"? Thx in advance Markus
Nov 21 2017
On Tuesday, 21 November 2017 at 23:12:33 UTC, Markus wrote:hi, im trying to interface a cpp class. I'd like to interface a bigger library and I'm trying to figure out the minimum effort.Possibly it will be interesting https://www.youtube.com/watch?v=HTgJaRRfLPk
Nov 22 2017
On Wednesday, 22 November 2017 at 08:29:26 UTC, MGW wrote:Possibly it will be interestinghttps://pp.userapi.com/c639524/v639524332/60240/uH3jnxrchik.jpg
Nov 22 2017
22.11.2017 02:12, Markus пишет: snipI could do the instancing/destruction by functions and write a custom d class that calls these methods in this()/~this().This is what I used to do as special members like ctor/dtor did not supported in D before, but your example of using ctor is interesting. What about dtor - you allocate class using D GC but try to destroy it manually - namely this I guess gives you an error in rt_finalize2 because it tries to destroy object that has been destroyed.
Nov 22 2017
On Wednesday, 22 November 2017 at 08:43:54 UTC, drug wrote:22.11.2017 02:12, Markus пишет: What about dtor - you allocate class using D GC but try to destroy it manually - namely this I guess gives you an error in rt_finalize2 because it tries to destroy object that has been destroyed.hmm... I'm not sure. I compiled dmd and it's druntime in debug and I'm stepping through it. Seems there has to be a "ClassInfo" filled with data. In my case it's filled with garbage. So the call ClassInfo.destructor fails, because destructor is an invalid pointer. The GC.collect works, but doesn't call the destructor. https://github.com/dlang/druntime/blob/3d8d4a45c01832fb657c16a656b6e1566d77fb21/src/rt/lifetime.d#L1391 I know constructors and destructors are not supported. And I get that point for move/copy... constructors. But I don't get it for simple construction and destruction. Markus
Nov 22 2017
another indicator (as documented) that GC destructor won't work // extern(C++) classes don't have a classinfo pointer in their vtable so the GC can't finalize them https://github.com/dlang/druntime/blob/3d8d4a45c01832fb657c16a656b6e1566d77fb21/src/rt/lifetime.d#L90 annoying :( Markus
Nov 22 2017
22.11.2017 19:06, Markus пишет:another indicator (as documented) that GC destructor won't work // extern(C++) classes don't have a classinfo pointer in their vtable so the GC can't finalize them https://github.com/dlang/druntime/blob/3d8d4a45c01832fb657c16a656b6e1566d77fb21/s c/rt/lifetime.d#L90 annoying :( MarkusI've made a guess that more c++ support were added in the last frontend version, well I was wrong
Nov 22 2017