为什么这个`类型`包装函数的返回值是 ctypes
封装函数 c_long(0)
而不是 c_long(3)
?
// main.cpp
#include <iostream>
class AComplicatedCPPObj {
int *d_;
public:
explicit AComplicatedCPPObj(int *d)
: d_(d) {
}
int *getD() const {
return d_;
}
void setD(int *d) {
d_ = d;
}
};
#ifdef CTYPESTEST_EXPORTS
#if defined(_WIN64)
#define CTYPESTEST_API __declspec(dllexport)
#else
#define CTYPESTEST_API __declspec(dllimport)
#endif
#endif
extern "C" {
CTYPESTEST_API AComplicatedCPPObj *AComplicatedCPPObj_new(int *d) {
return new AComplicatedCPPObj(d);
}
CTYPESTEST_API int *AComplicatedCPPObj_getD(AComplicatedCPPObj *aComplicatedCppObj) {
return aComplicatedCppObj->getD();
}
CTYPESTEST_API void AComplicatedCPPObj_setD(AComplicatedCPPObj *aComplicatedCppObj, int *d) {
aComplicatedCppObj->setD(d);
}
}
编译成共享库的cmake脚本。
cmake_minimum_required(VERSION 3.15)
project(ctypesTest)
set(CMAKE_CXX_STANDARD 14)
add_library(ctypesTest SHARED main.cpp)
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/INSTALL)
add_definitions("-DCTYPESTEST_EXPORTS=TRUE")
install(TARGETS ctypesTest)
而在python端。
import ctypes as ct
import os, glob, sys
WORKING_DIRECTORY = os.path.dirname(__file__)
# find the shared library
if sys.platform == "linux":
so_path = os.path.join(WORKING_DIRECTORY, "INSTALL/lib/libctypesTest.so")
else:
so_path = os.path.join(WORKING_DIRECTORY, "INSTALL/bin/ctypesTest.dll")
if not os.path.isfile(so_path):
raise FileNotFoundError(so_path)
# load sharedd library into python
lib = ct.CDLL(so_path)
class AComplicatedCPPObj:
def __init__(self, d):
self.set_function_types("AComplicatedCPPObj_new", [ct.POINTER(ct.c_int)], ct.c_void_p)
# checking some ctyeps functions
print("d", d, ct.c_int(d), ct.pointer(ct.c_int(d)), ct.pointer(ct.c_int(d)).contents)
self.obj = lib.AComplicatedCPPObj_new(ct.pointer(ct.c_int(d)))
self.set_function_types("AComplicatedCPPObj_getD", [ct.c_void_p], ct.POINTER(ct.c_long))
self.set_function_types("AComplicatedCPPObj_setD", [ct.c_void_p, ct.POINTER(ct.c_long)], None)
def set_function_types(self, func_name, argtypes, restype):
f = lib.__getattr__(func_name)
f.argtypes = argtypes
f.restype = restype
def getD(self):
return lib.AComplicatedCPPObj_getD(self.obj).contents
def setD(self, d):
if isinstance(d, str):
d = d.encode()
return lib.AComplicatedCPPObj_setD(self.obj, d)
if __name__ == '__main__':
o = AComplicatedCPPObj(3)
print(o.getD())
返回
d 3 c_long(3) <__main__.LP_c_long object at 0x000001F66F0E8248> c_long(3)
c_long(0)
当我希望看到
d 3 c_long(3) <__main__.LP_c_long object at 0x000001F66F0E8248> c_long(3)
c_long(3)
我不知道我现在做了什么不同的事情,但现在我重新运行这段代码时得到的输出是这样的。
d 3 c_long(3) <__main__.LP_c_long object at 0x000002320B8A52C8> c_long(3)
c_long(193626992)
另外,根据注释中的一个问题,当我运行时:
x = 3
o = AComplicatedCPPObj(x)
print(o.getD())
输出是
d 3 c_long(3) <__main__.LP_c_long object at 0x000002320B8A5248> c_long(3)
c_long(193627312)
这仍然不是
d 3 c_long(3) <__main__.LP_c_long object at 0x000002320B8A52C8> c_long(3)
c_long(3)
?
在这一行中,你创建了一个临时指针,指向一个临时对象。
self.obj = lib.AComplicatedCPPObj_new(ct.pointer(ct.c_int(d)))
这一行执行完后, pointer
和 c_int
对象都被释放。 所以你有未定义的行为。 传递给函数的指针已经不存在了。
我不知道你为什么要把指向对象的指针传来传去,但是为了解决你目前的行为,你需要保留一个指向 c_int
对象,只要C++代码持有一个指向它的指针就可以了。
import ctypes as ct
lib = ct.CDLL('./ctypesTest')
class AComplicatedCPPObj:
def __init__(self, d):
self.set_function_types("AComplicatedCPPObj_new", [ct.POINTER(ct.c_int)], ct.c_void_p)
self.set_function_types("AComplicatedCPPObj_getD", [ct.c_void_p], ct.POINTER(ct.c_int))
self.set_function_types("AComplicatedCPPObj_setD", [ct.c_void_p, ct.POINTER(ct.c_int)], None)
# keep reference to object and pass by reference to C++
self.param = ct.c_int(d)
self.obj = lib.AComplicatedCPPObj_new(ct.byref(self.param))
def set_function_types(self, func_name, argtypes, restype):
f = lib.__getattr__(func_name)
f.argtypes = argtypes
f.restype = restype
def getD(self):
return lib.AComplicatedCPPObj_getD(self.obj).contents
def setD(self, d):
# update local object and pass again by reference
self.param = ct.c_int(d)
return lib.AComplicatedCPPObj_setD(self.obj, ct.byref(self.param))
if __name__ == '__main__':
o = AComplicatedCPPObj(3)
print(o.getD())
但为什么不直接把整数的值传递给它 这样就不用跟踪内存了?
更新了.cpp。
#include <iostream>
class AComplicatedCPPObj {
int d_;
public:
explicit AComplicatedCPPObj(int d)
: d_(d) {
}
int getD() const {
return d_;
}
void setD(int d) {
d_ = d;
}
};
// FYI, _WIN32 is defined for both 32-bit and 64-bit windows, unlike _WIN64.
#if defined(_WIN32)
# define CTYPESTEST_API __declspec(dllexport)
#else
# define CTYPESTEST_API __declspec(dllimport)
#endif
extern "C" {
CTYPESTEST_API AComplicatedCPPObj *AComplicatedCPPObj_new(int d) {
return new AComplicatedCPPObj(d);
}
CTYPESTEST_API int AComplicatedCPPObj_getD(AComplicatedCPPObj *aComplicatedCppObj) {
return aComplicatedCppObj->getD();
}
CTYPESTEST_API void AComplicatedCPPObj_setD(AComplicatedCPPObj *aComplicatedCppObj, int d) {
aComplicatedCppObj->setD(d);
}
}
更新了Python:
import ctypes as ct
lib = ct.CDLL('./ctypesTest')
def set_function_types(func_name, argtypes, restype):
f = lib.__getattr__(func_name)
f.argtypes = argtypes
f.restype = restype
class AComplicatedCPPObj:
def __init__(self, d):
set_function_types("AComplicatedCPPObj_new", [ct.c_int], ct.c_void_p)
set_function_types("AComplicatedCPPObj_getD", [ct.c_void_p], ct.c_int)
set_function_types("AComplicatedCPPObj_setD", [ct.c_void_p, ct.c_int], None)
self.obj = lib.AComplicatedCPPObj_new(d)
def getD(self):
return lib.AComplicatedCPPObj_getD(self.obj)
def setD(self, d):
return lib.AComplicatedCPPObj_setD(self.obj, d)
if __name__ == '__main__':
o = AComplicatedCPPObj(3)
print(o.getD())