Memory leak in Python extension when array is created with PyArray_SimpleNewFromData() and returned

前端 未结 1 1273
一个人的身影
一个人的身影 2021-02-06 06:02

I wrote a simple Python extension module to simulate a 3-bit analog-to-digital converter. It is supposed to accept a floating-point array as its input to return the same size ar

相关标签:
1条回答
  • 2021-02-06 06:29

    The problem is not with PyArray_SimpleNewFromData which produces a properly refcounted PyObject*. Rather, it's with your malloc, assigned to pout then never freed.

    As the docs at http://docs.scipy.org/doc/numpy/user/c-info.how-to-extend.html clearly state, documenting PyArray_SimpleNewFromData:

    the ndarray will not own its data. When this ndarray is deallocated, the pointer will not be freed. ... If you want the memory to be freed as soon as the ndarray is deallocated then simply set the OWNDATA flag on the returned ndarray.

    (my emphasis on the not). IOW, you're observing exactly the "will not be freed" behavior so clearly documented, and are not taking the step specifically recommended should you want to avoid said behavior.

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