Thread overview
[Issue 15626] extern(C++) calling crash
Jan 30, 2016
Walter Bright
Jan 30, 2016
Walter Bright
Feb 01, 2016
Walter Bright
Feb 05, 2016
Walter Bright
Feb 05, 2016
Walter Bright
January 30, 2016
https://issues.dlang.org/show_bug.cgi?id=15626

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> ---
Thanks, Manu. The first thing I notice is main() is in C++. This will not work because it will bypass the D runtime initialization. That isn't what this bug is about, though. I'll see what I can do with this.

--
January 30, 2016
https://issues.dlang.org/show_bug.cgi?id=15626

--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> ---
Stripping away the unnecessary template and complex names:

---------- D -----------------
extern(C++)
{
    class C { }

    interface I
    {
        void f();
    }

    abstract class Base : I
    {
        int instance;
    }

    void test(Base b)
    {
        b.f();
    }

    void mainx();
}

int main()
{
    mainx();
    return 0;
}

------------- C++ ------------
#include <stdio.h>

class C { };

class I
{
    virtual void f() = 0;
};

class Base : public I
{
  public:
    int pInstance;

    Base() { pInstance = 0xBAADF00D; }
};

class Derived : public Base
{
  public:
    void f() { printf("!!"); }
};

void test(Base *b);

void mainx()
{
    Derived *d = new Derived;
    test(d);
}

--
February 01, 2016
https://issues.dlang.org/show_bug.cgi?id=15626

--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> ---
As a workaround, if you create a base class for 'Base' that contains a virtual function, in both the D and C++ code, it will work.

--
February 05, 2016
https://issues.dlang.org/show_bug.cgi?id=15626

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |C++

--
February 05, 2016
https://issues.dlang.org/show_bug.cgi?id=15626

--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> ---
https://github.com/D-Programming-Language/dmd/pull/5403

There's no easy way to hack this in. The trouble is you have a C++ base class with no virtual functions, so it has no vtbl[]. I.e. is it not a class at all, it is a struct. This is not supported.

The way to fix the code is to replace interface I with abstract class I, move the fields from C into I, and ditch class C entirely.

--
February 06, 2016
https://issues.dlang.org/show_bug.cgi?id=15626

--- Comment #5 from github-bugzilla@puremagic.com ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/6d01b29c86aab514cf40d6657bad866c9f7fb4e8 fix Issue 15626 - extern(C++) calling crash

https://github.com/D-Programming-Language/dmd/commit/bbbba7b358a2824bab8421b0455b613d2b899f40 Merge pull request #5403 from WalterBright/fix15626

fix Issue 15626 - extern(C++) calling crash

--
February 06, 2016
https://issues.dlang.org/show_bug.cgi?id=15626

github-bugzilla@puremagic.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--