Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in get_dft_array #577

Merged
merged 4 commits into from
Oct 23, 2018
Merged

Fix memory leak in get_dft_array #577

merged 4 commits into from
Oct 23, 2018

Conversation

ChristopherHogan
Copy link
Contributor

python/meep.i Outdated
@@ -372,6 +372,7 @@ PyObject *_get_dft_array(meep::fields *f, dft_type dft, meep::component c, int n
}

PyObject *py_arr = PyArray_SimpleNewFromData(rank, arr_dims, NPY_CDOUBLE, dft_arr);
PyArray_ENABLEFLAGS((PyArrayObject*)py_arr, NPY_ARRAY_OWNDATA);
Copy link
Collaborator

@stevengj stevengj Oct 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will eventually cause NumPy to call free, so it is not safe to set this flag for an array allocated with new like in get_dft_array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to be able to get the array size and allocate a numpy array from Python like we do in Simulation.get_array, but I don't see a way to get the dft array size, so I'm copying the dft data to my own malloced array.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to set the NPY_ARRAY_OWNDATA flag if you are using PyArray_SimpleNew

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Fixed.

python/meep.i Outdated
}

PyObject *py_arr = PyArray_SimpleNewFromData(rank, arr_dims, NPY_CDOUBLE, dft_arr);
// Allocate our own version of dft_arr with malloc because
// numpy will eventually call free on it.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it would be simpler to just do:

PyObject *py_arr = PyArray_SimpleNew(rank, arr_dims, NPY_CDOUBLE);
memcpy(PyArray_DATA(py_arr), dft_arr, sizeof(std::complex<double>) * length);

i.e. to let Python do the allocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@coveralls
Copy link

coveralls commented Oct 22, 2018

Coverage Status

Coverage increased (+0.04%) to 81.977% when pulling c8d335f on ChristopherHogan:chogan/npy_mem_leak into 80e0ec4 on stevengj:master.

@stevengj stevengj merged commit e5dd124 into NanoComp:master Oct 23, 2018
@ChristopherHogan ChristopherHogan deleted the chogan/npy_mem_leak branch October 31, 2018 13:45
bencbartlett pushed a commit to bencbartlett/meep that referenced this pull request Sep 9, 2021
* Fix memory leak in get_dft_array

* Copy dft array so numpy can free it

* Let numpy allocate via PyArray_SimpleNew

* OWNDATA flag is already true with PyArray_SimpleNew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants