Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: fix memory leak in np.zeros when fill-zero loop raises (#31320)#31357

Merged
charris merged 1 commit into
numpy:maintenance/2.4.xfrom
charris:backport-31320
Apr 28, 2026
Merged

BUG: fix memory leak in np.zeros when fill-zero loop raises (#31320)#31357
charris merged 1 commit into
numpy:maintenance/2.4.xfrom
charris:backport-31320

Conversation

@charris
Copy link
Copy Markdown
Member

@charris charris commented Apr 28, 2026

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

BUG: fix memory leak in np.zeros when fill-zero loop raises
@charris charris added this to the 2.4.5 release milestone Apr 28, 2026
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels Apr 28, 2026
@charris
Copy link
Copy Markdown
Member Author

charris commented Apr 28, 2026

Blas failures are unrelated.

@charris charris merged commit b2b53f3 into numpy:maintenance/2.4.x Apr 28, 2026
77 of 79 checks passed
@charris charris deleted the backport-31320 branch April 28, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 08 - Backport Used to tag backport PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants