Deallocating Memory For Objects Returned To Python Through Ctypes
I am using ctypes for extending my c functions in MyDll to python. from ctypes import cdll libX = cdll.LoadLibrary('d:\\MyTestProject\\debug\\MyDll.dll') further in the .py file
Solution 1:
I typically handle this by adding an extern destroy object function that I can pass the pointer back to and delete it.
CPP:
SomeClass* createObj()
{
returnnewSomeClass();
}
voiddestroyObj(SomeClass* pObj){
delete pObj;
pObj = NULL;
}
H:
extern"C" {
SomeClass* createObj();
voiddestroyObj(SomeClass*);
}
PY:
classSomeObj:def__init__(self):
self.soLib = cdll.LoadLibrary(PATH_SO)
_createObj = self.soLib.createObj
_createObj.restype = POINTER(c_long)
self._objRef = _createObj()
## do stuff with self._objRefdef__del__(self):
self.soLib.destroyObj(self._objRef)
Post a Comment for "Deallocating Memory For Objects Returned To Python Through Ctypes"