Skip to content

Conversation

charris
Copy link
Member

@charris charris commented Jul 22, 2025

Backport of #29358.

Fixes #29355.

We already increment a reference to out in _set_out_array (in ufunc_object.c):

c
static int
_set_out_array(PyObject *obj, PyArrayObject **store)
{
    ...
    Py_INCREF(obj);
    *store = (PyArrayObject *)obj;

    return 0;
    ...
}

This PR removes additional increments and adds a missing decrement that caused the memory leakage.

…umpy#29358)

Add back missing DECREF for `out=` in ufunc.reduce (and etc.) when `out` is passed.

* BUG: add Py_XDECREF to `out` for failure case in generic reduction

* TST: add reference count checks for `out` in reduction leak tests
@charris charris added this to the 2.3.2 release milestone Jul 22, 2025
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs labels Jul 22, 2025
@charris charris changed the base branch from main to maintenance/2.3.x July 22, 2025 15:31
@charris charris changed the title Backport 29358 BUG: Fix reference leakage for output arrays in reduction functions Jul 22, 2025
@charris charris merged commit db022e2 into numpy:maintenance/2.3.x Jul 22, 2025
10 checks passed
@charris charris deleted the backport-29358 branch July 22, 2025 17:03
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.

BUG: Memory leak using out= parameter in numpy version 2.3
2 participants