BUG: fix memory leak in np.zeros when fill-zero loop raises (#31320)#31357
Merged
Conversation
BUG: fix memory leak in np.zeros when fill-zero loop raises
Member
Author
|
Blas failures are unrelated. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Backport of #31320.
BUG: fix memory leak in np.zeros when fill-zero loop raises
PR summary
This PR fixes a memory leak in np.zeros that occurs when the fill-zero loop raises an exception (which can happen for custom user-defined dtypes). It does this by setting NPY_ARRAY_OWNDATA earlier to ensure the data will be deallocated, combined with changes to mem_handler cleanup to avoid a double-decref.
I'm not entirely sure whether this is the most appropriate way to fix the leak, but it does seem to work correctly based on testing with
tracemalloc.AI Disclosure
The bug was detected through testing with
tracemalloc, then located and fixed by Claude Opus 4.7.Full AI-generated description: https://gist.github.com/MaartenBaert/f3eadd280f84ef6896586025f59788ec