Create objects in C++ and pass pointers to python
I’m trying to share C++ objects with Python using ctypes by creating the object in C++ and then passing pointers to Python via a C wrapper. I hope to be able to manipulate the object later using other functions in the Python class (do_something
in the following code).
I tried the code below but got a segfault. I’m not familiar with C vs. C++ and C vs. Python interfaces, so I’m not sure if I’m making some fundamental mistake when passing pointers, or if after me, memory is being cleared/offloaded by Python garbage collection to create objects?
This question is discussed Similar question for boost, but the answer is not very useful for ctypes.
object.h
class object {
public:
constructor
object() {
pointer = nullptr;
}
destructor
virtual ~object() {
delete pointer;
pointer = nullptr;
}
get member functions of object_pointer
from C++
double do_something();
protected:
pointer to the object
object_pointer *pointer;
};
extern "C" {
object* object_new();
void object_delete(object *Ob);
double object_do_something(object *Ob);
}
Object .cpp
#include "object.h"
double object::do_something() { return pointer->do_something(); }
extern "C" {
object *object_new() { return new object(); }
void object_delete(object *Ob) { delete Ob; }
double object_do_something(object *Ob) { return Ob->do_something(); }
}
Object .py
from ctypes import *
lib = cdll.LoadLibrary('./lib_object.so')
lib.object_new.argtypes = ()
lib.object_new.restype = c_void_p
lib.special_delete.argtypes = c_void_p,
lib.special_delete.restype = None
lib.object_pointer.argtypes = c_void_p
lib.object_pointer.restype = c_void_p
class Object:
def __init__(self):
self.obj = lib.object_new()
print self.obj
def __del__(self):
lib.object_delete(self.obj)
def do_something(self):
lib.object_do_something(self.obj)
s = Object()
>> 94549743086144
s.do_something()
>> Segfault
Any help would be appreciated!
Solution
Notes:
- All files (.cpp, .py) in the issue are not compiled. They contain syntax errors as well as semantic errors
- I don’t know what the pointer does (it produces a syntax error). I can only assume that a singleton implementation was attempted
- With the above in mind, instead of pointing out the errors in the existing file (there are a lot of them), I created a whole new basic example
- aren’t the only areas for improvement, I did point it out: [Python 2]: ctypes – A foreign function library for Python
While ctypes
object.h:
class Object {
public:
Object() {
m_double = 2.718282;
}
virtual ~Object() {}
double do_something();
private:
double m_double;
};
extern "C" {
Object* object_new();
void object_delete(Object *ob);
double object_do_something(Object *ob);
}
object.cpp:
<pre class=”lang-c prettyprint-override”>#include <iostream>
#include "object.h"
using std::cout;
double Object::do_something() {
std::cout << "Doing something in C++\n";
return m_double;
}
extern "C" {
Object *object_new() { return new Object(); }
void object_delete(Object *pObj) { delete pObj; }
double object_do_something(Object *pObj) { return pObj->do_something(); }
}
object.py:
#!/usr/bin/env python2
import sys
import ctypes
lib = ctypes.cdll.LoadLibrary('./lib_object.so')
lib.object_new.argtypes = []
lib.object_new.restype = ctypes.c_void_p
lib.object_delete.argtypes = [ctypes.c_void_p]
lib.object_do_something.argtypes = [ctypes.c_void_p]
lib.object_do_something.restype = ctypes.c_double
class Object:
def __init__(self):
self.obj = lib.object_new()
print("`Object` instance (as a `void *`): 0x{:016X}".format(self.obj))
def __del__(self):
lib.object_delete(self.obj)
def do_something(self):
return lib.object_do_something(self.obj)
def main():
obj = Object()
ret = obj.do_something()
print(ret)
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
Output:
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054594122]> ls object.cpp object.h object.py [cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054594122]> gcc -shared -fPIC -o lib_object.so object.cpp -lstdc++ [cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q054594122]> python2 object.py Python 2.7.14 (default, Oct 31 2017, 21:12:13) [GCC 6.4.0] on cygwin `Object` instance (as a `void *`): 0x0000000600078870 Doing something in C++ 2.718282