I've been experiencing a memory leak when calling some C code I wrote from Python 3.10.6 on Ubuntu 22.04.1 LTS.
https://github.com/seung-lab/mapbuffer/blob/main/mapbufferaccel.c#L95-L110
When used in isolation, there is no memory leak, but the following code appears to leak:
https://github.com/seung-lab/igneous/blob/master/igneous/tasks/mesh/multires.py#L390-L406
When I replace the C call with a python implementation, the leak disappears:
https://github.com/seung-lab/mapbuffer/blob/main/mapbuffer/mapbuffer.py#L197
This only happens on Linux, on MacOS there is no leak. I tried calling both the python and C versions at the same time and using the python result while deleting elements from the C version. I got the C version only returning -1 and it seems simply calling the C version causes the leak on Linux.
Any ideas what's going on? Is it some part of glibc that is causing a problem? Did I forget to call some cleanup function in the Python C API?
Here is the non-leaking Python implementation of that function:
def eytzinger_search(target, arr):
mid = 0
N = len(arr)
while mid < N:
if arr[mid] == target:
return mid
mid = mid * 2 + 1 + int(arr[mid] < target)
return -1
k = eytzinger_search(np.uint64(label), index[:, 0])
Thanks so much!