Skip to content

BUG: Choose (and probably more item selection) leaks references with out= #22683

Closed

Description

@seberg

Describe the issue:

np.choose internally uses raw access to the array data and a final PyArray_INCREF. That somewhat works generally, but if out= is specified, that result array would need to be cleared first.

The short-term solution here is likely that we need to use the element copy function rather than memcpy when necessary (elsewhere, we currently use copyswap if the dtype flags "needs API").

I suspect, we need to keep something like copyswap, but I need to also think about how to evolve that function.

Reproduce the code example:

import sys
import numpy as np
a = np.ones(10000, dtype=object)
sys.getrefcount(1)
np.choose(np.zeros(10000, dtype=int), [a], out=a)
sys.getrefcount(1)
np.choose(np.zeros(10000, dtype=int), [a], out=a)
sys.getrefcount(1)

# Prints (increasing by 10000 every time):
# 290281
# 300281
# 310281

Error message:

No response

NumPy/Python version information:

NumPy 1.25 and earlier

Context for the issue:

No response

Activity

  1. added 3 commits that reference this issue on Jul 15, 2023
    0f91f7d
    a1e5395
    c694a67
  2. added a commit that references this issue on Jul 31, 2023
    2f92f29
  3. added a commit that references this issue on Aug 3, 2023
    ec9c025
  4. added a commit that references this issue on Nov 11, 2023
    172c5c5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions