8000 MAINT: Use `ENUM_CHECK_NAME` for avoiding memory leaks in `_superluobject.c` by czgdp1807 · Pull Request #22071 · scipy/scipy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Use ENUM_CHECK_NAME for avoiding memory leaks in _superluobject.c #22071

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

Merged
merged 1 commit into from
May 16, 2025

Conversation

czgdp1807
Copy link
Member

Reference issue

Closes #16234

@Snape3058 I think this change increases code safety. It uses already existing macro ENUM_CHECK_NAME which always does Py_XDECREF(tmpobj) before returning. Regarding, manually destroying tmpobj, I think it's not safe. That pattern is also not used elsewhere in SciPy.

What does this implement/fix?

Additional information

@github-actions github-actions bot added scipy.sparse.linalg C/C++ Items related to the internal C/C++ code base DX Everything related to making the experience of working on SciPy more pleasant labels Dec 13, 2024
@lucascolley lucascolley added maintenance Items related to regular maintenance tasks and removed DX Everything related to making the experience of working on SciPy more pleasant labels Dec 13, 2024
@lucascolley lucascolley changed the title DEV: Use ENUM_CHECK_NAME for avoiding memory leaks in _superluobject.c MAINT: Use ENUM_CHECK_NAME for avoiding memory leaks in _superluobject.c Dec 13, 2024
@j-bowhay j-bowhay added this to the 1.16.0 milestone Dec 16, 2024
Copy link
Contributor
@tylerjereddy tylerjereddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way we could get some kind of measurable indication that memory leaks have genuinely been fixed here?

On the rebased version of this branch, using memory_profiler with an splu snippet

import numpy as np
from scipy.sparse import csc_array
from scipy.sparse.linalg import splu


for i in range(500000):
    A = csc_array([[1,2,0,4], [1,0,0,1], [1,0,2,1], [2,2,1,0.]])
    lu = splu(A)
    b = np.array([1, 2, 3, 4])
    x = lu.solve(b)
    A.dot(x)
    lu.perm_r
    lu.perm_c
image

On main at time of writing:

image

They both look stable to me, but I'm also fairly ignorant of the method to reproduce the memory leak here given that we just have a static analyzer report to go off.

Copy link
Member
@rgommers rgommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This minor issue was introduced in commit 77e88d7, which predates the introduction of ENUM_CHECK_NAME in commit f414439 almost 2 years later. So that explains why ENUM_CHECK_NAME wasn't used from the start.

This LGTM, simplifies the code and is more correct.

Is there a way we could get some kind of measurable indication that memory leaks have genuinely been fixed here?

It's a tiny leak in an error handling path so you can't hit it in a loop unless you somehow manage to insert a wrong name and then loop over a try-except; it doesn't matter in the real world.

@rgommers rgommers merged commit b19af0c into scipy:main May 16, 2025
37 of 38 checks passed
@rgommers
Copy link
Member

Thanks @czgdp1807,@Snape3058 and @tylerjereddy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C/C++ Items related to the internal C/C++ code base maintenance Items related to regular maintenance tasks scipy.sparse.linalg
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Memory leak in _superluobject.c when ENUM_CHECK is not used (static analyzer report)
5 participants
0