-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
BUG: Structured arrays do not clean up properly when freed, causing memory leaks #25949
Comments
This was just a typo, add a test for the regression (checked that it triggered it). Closes numpygh-25949
This was just a typo, add a test for the regression (checked that it triggered it). Closes numpygh-25949
Thanks for finding this and the (very!) detailed report. The annoying thing is it's just a simple typo, but not sure we will have another 1.26 release to backport it :(. A fix is in gh-25950. |
Thank you for the amazingly fast fix! As long as the fix appears in some 1.x release then I am happy. We can just tell our customers to upgrade their NumPy. |
Looks like another 1.26 release, which might be a pain at this point :) |
I am almost wondering if I should create "this is utterly terrible but works" hack to reach into the NumPy internals and fix things up for you? It seems like it might actually be the bettre solution here. |
If a hack is the only way then I am ok with that. The downside is that I will need to backport it to several supported releases of our extension... |
Here is the hack you can add. I guess the question is what's easier NumPy or your release, and at this point I am not sure:
EDIT: The function needs to be called after registration on the actual dtype instance of course. |
Thanks for that, it has resolved the issue |
This was just a typo, add a test for the regression (checked that it triggered it). Closes numpygh-25949
Describe the issue:
Given a structured array with a field of type object, when the array is freed, NumPy 1.24 and earlier would decref the objects in the array allowing them to be freed. Since 1.25 this no longer happens, so the objects are leaked. This was introduced in #23620 and persists in the latest release (1.26.4).
Summary of behaviour in 1.24:
PyArray_ClearBuffer
is called when the array is freedPyArray_GetClearFunction
returnsfunc=traverse_fields_function
withauxdata
set to afields_clear_data
struct withfields[0]
pointing toclear_object_strided_loop
Summary of behaviour in 1.25.0:
PyArray_ClearBuffer
is called when the array is freedPyArray_GetClearFunction
returnsfunc=zerofill_fields_function
withauxdata
set to afields_clear_data
struct withfields[0]
pointing tofill_zero_object_strided_loop
zerofill_fields_function
memsets the array item to zero, then callsfill_zero_object_strided_loop
, which copies aPyObject
representing zero into the array and increfs itThat last point should makes it clear that this is a bug: the new item is increfed, but the previous item is zeroed out without first decrefing.
Note that my dtype implements
fill
andfillwithscalar
: it would be natural to call these when the array is being zeroed, but they are not called.Reproduce the code example:
Error message:
No response
Python and NumPy Versions:
1.26.4
3.10.11 | packaged by conda-forge | (main, May 10 2023, 19:01:19) [Clang 14.0.6 ]
Runtime Environment:
Context for the issue:
We use structured dtypes in our commercial Python extension: https://pypi.org/project/xpress. The memory leaks are causing issues in customer environments.
The text was updated successfully, but these errors were encountered: