Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deallocating memory for objects returned to python through ctypes

Tags:

python

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.

like image 381
Abhaya Avatar asked Sep 13 '11 07:09

Abhaya


1 Answers

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)
like image 70
Mark Avatar answered Sep 23 '22 03:09

Mark