visual studio - C++ binary compatible dll POD class member initialization causes crash -


i'm trying make inter-compiler compatible class in dll built mingw can used in windows vs application. issue is, class crashes moment tries initialize member variable when function called vs program. using stl or making local variables within same functions works fine.

unhandled exception @ 0x6bec19fe (test.dll) in consoleapplication2.exe: 0xc0000005: access violation writing location 0x154eb01e.

simple demonstration:

the dll code

#include <iostream>  class tester  { public:      virtual void test() = 0; };  class testerimp : public tester { public:     testerimp(){}     ~testerimp(){}      virtual void test() {          int test = 5; // fine         m_test = 5; // access violation     }  private:      int m_test;  };      extern "c"     {         __declspec (dllexport) tester* create()         {             return new testerimp();         }     } 

main program loads dll , calls test function:

#include <iostream> #include <windows.h>   class tester  { public:      virtual void test() = 0; };  typedef tester* (*createptr)();  int main(int argc, const char **argv, char * const *envp) {    hinstance hgetprociddll = loadlibrarya("test.dll");    if (hgetprociddll == null) {     std::cout << "could not load dynamic library" << std::endl;     return exit_failure;   }    createptr ctr = (createptr)getprocaddress(hgetprociddll, "create");   if (!ctr) {     std::cout << "could not locate function" << std::endl;     return exit_failure;   }    std::cout << "dll loading success!" << std::endl;    tester* testf = ctr();    std::cout << "got test class" << std::endl;    testf->test(); // crash if member variable referenced, fine otherwise local variable initialization or stl use inside function    std::cout << "running dll functions success!" << std::endl;    return 0; } 

main program compiled in debug mode vs2012 , dll built using g++ mingw -

g++ -shared -o test.dll test.cpp

could explain behavior me, please?

thank you.

you're using older version of mingw. until 4.7.0 (i think), mingw passed this pointer on stack different msvc's thiscall convention of passing this pointer in ecx.

starting 4.7.0, mingw uses same thiscall calling convention msvc.

i can example testerimp::test() function called msvc marking both tester::test() , testerimp::test() in test.cpp __attribute__((thiscall)) attribute. worked mingw 4.6.2, cause crash without attribute being used.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -