Skip to content
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

Fix leak in nrnpy_store_savestate_. #3229

Merged
merged 1 commit into from
Nov 20, 2024
Merged

Fix leak in nrnpy_store_savestate_. #3229

merged 1 commit into from
Nov 20, 2024

Conversation

1uc
Copy link
Collaborator

@1uc 1uc commented Nov 20, 2024

Facts:

  • PyObject_CallObject returns a new reference 1.
  • PyByteArray_Size doesn't steal the reference 2.
  • PyByteArray_AsString doesn't steal the reference 3.

This is a leak because:

  • PyObject_CallObject returns a new reference: +1.
  • Py_INCREF increases the local reference count to +2.
  • PyByteArray_Size and PyByteArray_AsString don't steal the reference.
  • Py_DECREF reduces the local reference count to +1.

Therefore, there's 1 INCREF we can't pair up with a DECREF.

The revised version does:

  • Takes ownership of the new reference with nb::steal. Therefore, the local reference count is +0.
  • PyByteArray_Size and PyByteArray_AsString don't steal the reference.

Therefore, all INCREF can be paired with a DECREF.

Facts:
  * `PyObject_CallObject` returns a new reference [1].
  * `PyByteArray_Size` doesn't steal the reference [2].
  * `PyByteArray_AsString` doesn't steal the reference [3].

This is a leak because:
  * `PyObject_CallObject` returns a new reference: `+1`.
  * `Py_INCREF` increases the local reference count to  `+2`.
  * `PyByteArray_Size` and `PyByteArray_AsString` don't steal the
    reference.
  * `Py_DECREF` reduces the local reference count to `+1`.

Therefore, there's 1 INCREF we can't pair up with a DECREF.

The revised version does:
  * Takes ownership of the new reference with `nb::steal`. Therefore,
    the local reference count is `+0`.
  * `PyByteArray_Size` and `PyByteArray_AsString` don't steal the
    reference.

Therefore, all INCREF can be paired with a DECREF.

[1]: https://docs.python.org/3/c-api/call.html#c.PyObject_CallObject
[2]: https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_Size
[3]: https://docs.python.org/3/c-api/bytearray.html#c.PyByteArray_AsString
@1uc 1uc mentioned this pull request Nov 20, 2024
Copy link

sonarcloud bot commented Nov 20, 2024

@1uc 1uc marked this pull request as ready for review November 20, 2024 13:10
Copy link

codecov bot commented Nov 20, 2024

Codecov Report

Attention: Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.

Project coverage is 67.07%. Comparing base (c84f391) to head (9ba4a9d).
Report is 13 commits behind head on master.

Files with missing lines Patch % Lines
src/nrnpython/nrnpy_hoc.cpp 33.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3229      +/-   ##
==========================================
- Coverage   67.08%   67.07%   -0.01%     
==========================================
  Files         569      569              
  Lines      111190   111161      -29     
==========================================
- Hits        74590    74564      -26     
+ Misses      36600    36597       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

Copy link

✔️ 9ba4a9d -> Azure artifacts URL

@1uc 1uc merged commit e8255b9 into master Nov 20, 2024
35 checks passed
@1uc 1uc deleted the 1uc/fix-leak-store branch November 20, 2024 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants