Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

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 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()
{
    return new SomeClass();
}

void destroyObj(SomeClass* pObj){
  delete pObj;
  pObj = NULL;
}

H:

extern "C" {
  SomeClass* createObj();
  void destroyObj(SomeClass*);
}

PY:

class SomeObj:
    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._objRef

    def __del__(self):
        self.soLib.destroyObj(self._objRef)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...