I'm trying to build a python extension which has a function returns a np.array. But it breaks when compiled with MinGW32 but works with VS2019. I tried to comment the code setting the flag NPY_ARRAY_OWNDATA, then MinGW32 and VS2019 both work, but lead a memory leak.
(I guess VS-built numpy cannot free memory which malloc by MinGW32.)
Is it possible using MinGW32 with numpy?
static PyObject *py_Decode(PyObject *self, PyObject *args)
{
// Do something
char *buffer = malloc(1024 * 1024 * 2);
npy_intp dims[2] = {1024, 1024};
PyObject *ret = PyArray_SimpleNewFromData(2, dims, NPY_SHORT, buffer);
PyArray_ENABLEFLAGS((PyArrayObject *)ret, NPY_ARRAY_OWNDATA); // When commented, mingw32 works, but memory leak.
return ret;
}
I'm using:
- Python 3.8.3 32bit
- numpy 1.18.5
- VS2019 19.26.28806
- mingw32-gcc 9.2.0