I’m writing a small wrapper for a C library in Python with Ctypes, and I don’t know if the structures allocated from Python will be automatically freed when they’re out of scope.
Example:
from ctypes import *
mylib = cdll.LoadLibrary("mylib.so")
class MyPoint(Structure):
_fields_ = [("x", c_int), ("y", c_int)]
def foo():
p = MyPoint()
#do something with the point
foo()
Will that point still be “alive” after foo returns? Do I have to call clib.free(pointer(p))? or does ctypes provide a function to free memory allocated for C structures?
In this case your
MyPointinstance is a Python object allocated on the Python heap, so there should be no need to treat it differently from any other Python object. If, on the other hand, you got theMyPointinstance by calling sayallocate_point()inmylib.so, then you would need to free it using whatever function is provided for doing so, e.g.free_point(p)inmylib.so.