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 i have a class the methods of which call the functions in MyDll through ctypes.
Class MyTestClass:
def __init__(self,id):
libA.MyTestClassInDLL_new.restype = ctypes.c_void_p
self.obj = libA.MyTestClassInDLL_new(id)
the corresponding c function MyTestClassInDLL_new has been defined in MyDll as -
extern "C" __declspec(dllexport) void * MyTestClassInDLL_new(char* id)
{
pTestObject = new CMyTestClassInDLL(CString(id));
return (void *)pTestObject;
}
Note I am using new to instantiate this object in my vc++ dll and returning the pointer to it. I have set the restype of this function in the .py file as ctypes.c_void_p.
The script that I execute contains the following -
testob = MyTestClass("5")
this works fine. the testob that i obtain here is used further to call its methods which internally call the c functions from MyDll.
However the object was created using new in MyDll and returned through MyTestClassInDLL_new() function. How is this object to be destroyed ? somewhere i need to use delete pTestObject so that its destructor is called which does the cleanup and the memory is deallocated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…