Safer way to expose a C-allocated memory buffer using numpy/ctypes?
You have to keep a reference to your Wrapper while any numpy array exists. Easiest way to achieve this, is to save this reference in a attribute of the ctype-buffer: class MyWrapper(object): def __init__(self, n=10): # buffer allocated by external library self.size = n self.addr = libc.malloc(C.sizeof(C.c_int) * n) def __del__(self): # buffer freed by … Read more