可以ctypes
用析构函数包装返回 C++ 类的对象(不是指针/引用)的函数吗?下面的示例在调用时会出现段错误lib.init_point_by_value
:
foo.cpp:
#include <iostream>
extern "C" {
using namespace std;
struct Point {
int x;
int y;
~Point();
};
Point::~Point() {
cout << "Point destructor called" << endl;
}
Point init_point_by_value(int x, int y) {
cout << "init_point_by_value called" << endl;
Point p;
p.x = x;
p.y = y;
return p;
}
Point& init_point_by_ref(int x, int y) {
cout << "init_point_by_ref called" << endl;
Point* p = new Point;
p->x = x;
p->y = y;
return *p;
}
void cleanup_point(Point* point) {
cout << "cleanup_point called" << endl;
if (point) {
delete point;
}
}
}
foo.py:
import ctypes
class Point(ctypes.Structure):
_fields_ = [
('x', ctypes.c_int),
('y', ctypes.c_int),
]
def setup_lib(lib_path):
lib = ctypes.cdll.LoadLibrary(lib_path)
lib.cleanup_point.argtypes = [ctypes.POINTER(Point)]
lib.init_point_by_value.argtypes = [ctypes.c_int, ctypes.c_int]
lib.init_point_by_value.restype = ctypes.POINTER(Point)
lib.init_point_by_ref.argtypes = [ctypes.c_int, ctypes.c_int]
lib.init_point_by_ref.restype = ctypes.POINTER(Point)
return lib
lib = setup_lib('./foolib.so')
p1 = lib.init_point_by_ref(3, 4)
lib.cleanup_point(p1)
# seg faults
p2 = lib.init_point_by_value(5, 6)
lib.cleanup_point(p2)
编译并运行它:
g++ -c -fPIC foo.cpp -o foo.o && g++ foo.o -shared -o foolib.so && python foo.py
输出:
init_point_by_ref called
cleanup_point called
Point destructor called
init_point_by_value called
Segmentation fault (core dumped)