I have the following shared object:
MyLib.cpp
#include <iostream>
class MyClass
{
public:
MyClass(){}
void function()
{
std::cout << "hello" << std::endl;
//var = 10;
}
private:
int var;
};
extern "C" {
MyClass* create()
{
return new MyClass();
}
void func(MyClass* myclass)
{
myclass->function();
}
}
That I compile with: g++ -fPIC -shared -o MyLib.so MyLib.cpp
I then use it with the following Python script:
script.py
import ctypes
lib = ctypes.cdll.LoadLibrary("./MyLib.so")
MyClass = lib.create()
lib.func(MyClass)
Like this, it works perfectly, but if I uncomment the line //var = 10;
, Python makes a segmentation fault (Python 3.8). This happens every time the object MyClass
makes a change to one of its local variable (except inside the constructor, where it works). It looks like the address of the variable var
is wrong and when accessing it, there is a segmentation fault. I tried using the keyword "virtual" for function
without any change, and I tried to import the shared object in another C++ program using dlfcn, which worked fine. Any idea what is wrong ?
create()
andfunc()
don't really know or care about the type of the object they use. The interface should probably returnvoid*
as an indicator of this. Thefunc()
function can the cast it back to the correct type before calling the method. – Cardenas