Fixed memory leak in contour #6942
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixed memory leak in contour as reported in #6940. PR is based on v2.x branch, but fix needs to be in all active branches.
Problem is down to my stupidity when adding numpy arrays to python lists to return from contour/contourf calls. New contouring C++ code uses our numpy
array_view
wrappers, which are in charge of their own reference counting, and native Python/C API list functions for which I am explicitly responsible for reference counting. Consider code:This is fine, but I am using
PyList_Append
instead ofPyList_SetItem
as I don't know the length of the list when it is created. It turns out thatPyList_Append
increments the reference count whereasPyList_SetItem
steals a reference. I assume I used to know this but have since forgotten it and have just rediscovered it.There are 2 options for the fix. Either
array_view.pyobj()
but store the returned object and decrement the reference count myself, orarray_view.pyobj_steal()
that returns a stolen reference.I've opted for the second option as that keeps all the explicit array reference counting within the
array_view
class.