Python ctype,C++对象销毁
考虑以下python ctype-c++绑定:
// C++
class A
{
public:
void someFunc();
};
A* A_new() { return new A(); }
void A_someFunc(A* obj) { obj->someFunc(); }
void A_destruct(A* obj) { delete obj; }
# python
from ctypes import cdll
libA = cdll.LoadLibrary(some_path)
class A:
def __init__(self):
self.obj = libA.A_new()
def some_func(self):
libA.A_someFunc(self.obj)
删除C++对象的最佳方式是什么,因为不再需要该对象。
[编辑] 我添加了建议的删除函数,但问题仍然是由谁以及何时调用该函数。应该尽可能方便。
解决方案
您可以实现__del__
方法,该方法调用您必须定义的析构函数:
C++
class A
{
public:
void someFunc();
};
A* A_new() { return new A(); }
void delete_A(A* obj) { delete obj; }
void A_someFunc(A* obj) { obj->someFunc(); }
Python
from ctypes import cdll
libA = cdll.LoadLibrary(some_path)
class A:
def __init__(self):
fun = libA.A_new
fun.argtypes = []
fun.restype = ctypes.c_void_p
self.obj = fun()
def __del__(self):
fun = libA.delete_A
fun.argtypes = [ctypes.c_void_p]
fun.restype = None
fun(self.obj)
def some_func(self):
fun = libA.A_someFunc
fun.argtypes = [ctypes.c_void_p]
fun.restype = None
fun(self.obj)
还请注意,您遗漏了__init__
方法上的self
参数。此外,您必须显式指定返回类型/参数类型,因为ctype默认为32位整数,而在现代系统上指针可能为64位。
有些人认为__del__
是邪恶的。或者,您可以使用with
语法:
class A:
def __init__(self):
fun = libA.A_new
fun.argtypes = []
fun.restype = ctypes.c_void_p
self.obj = fun()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
fun = libA.delete_A
fun.argtypes = [ctypes.c_void_p]
fun.restype = None
fun(self.obj)
def some_func(self):
fun = libA.A_someFunc
fun.argtypes = [ctypes.c_void_p]
fun.restype = None
fun(self.obj)
with A() as a:
# Do some work
a.some_func()
相关文章