title: deallocating memory for objects returned to python through ctypes
body: <p>I am using ctypes for extending my c functions in MyDll  to python. </p>
  
  <pre><code>from ctypes import cdll
  libX = cdll.LoadLibrary("d:\\MyTestProject\\debug\\MyDll.dll")
  </code></pre>
  
  <p>further in the .py file i have a class the methods of which call the functions in MyDll through ctypes.</p>
  
  <pre><code>Class MyTestClass:
         def __init__(self,id):
             libA.MyTestClassInDLL_new.restype = ctypes.c_void_p
             self.obj = libA.MyTestClassInDLL_new(id)
  </code></pre>
  
  <p>the corresponding c function MyTestClassInDLL_new has been defined in MyDll as -</p>
  
  <pre><code>extern "C" __declspec(dllexport) void * MyTestClassInDLL_new(char* id) 
  { 
       pTestObject = new CMyTestClassInDLL(CString(id)); 
       return (void *)pTestObject;    
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>The script that I execute contains the following -</p>
  
  <pre><code>testob = MyTestClass("5")
  </code></pre>
  
  <p>this works fine. the testob that i obtain here is used further to call its methods which internally call the c functions from MyDll.</p>
  
  <p>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. </p>