Creating a numpy array in C from an allocated array is causing memory leaks

前端 未结 1 1321
渐次进展
渐次进展 2021-01-13 05:50

I have traced a memory leak in my program to a Python module I wrote in C to efficiently parse an array expressed in ASCII-hex. (e.g. \"FF 39 00 FC ...\")

ch         


        
相关标签:
1条回答
  • 2021-01-13 06:31

    It's probably a reference-count issue (from How to extend NumPy):

    One common source of reference-count errors is the Py_BuildValue function. Pay careful attention to the difference between the ‘N’ format character and the ‘O’ format character. If you create a new object in your subroutine (such as an output array), and you are passing it back in a tuple of return values, then you should most- likely use the ‘N’ format character in Py_BuildValue. The ‘O’ character will increase the reference count by one. This will leave the caller with two reference counts for a brand-new array. When the variable is deleted and the reference count decremented by one, there will still be that extra reference count, and the array will never be deallocated. You will have a reference-counting induced memory leak. Using the ‘N’ character will avoid this situation as it will return to the caller an object (inside the tuple) with a single reference count.

    0 讨论(0)
提交回复
热议问题