Skip to content

Commit

Permalink
Fix leak in object_to_double_. (#3235)
Browse files Browse the repository at this point in the history
The fact:
  * `nrnpy_ho2po` seems to return a "new reference".
  * `PyFloat_AsDouble` doesn't steal [1].

Therefore, the previous version had:
  * A local reference count of `+1` after `ho2po`.
  * A local reference count of `+2` after `INCREF`.
  * The `PyFloat_AsDouble` doesn't change the reference count.
  * A local reference count of `+1` after `DECREF`.

Leaving one INCREF that we can't pair up with a DECREF.

The proposed version immediately takes ownership of the new reference
returned by `nrnpy_ho2po`.

[1]: https://docs.python.org/3/c-api/float.html#c.PyFloat_AsDouble
  • Loading branch information
1uc authored Nov 21, 2024
1 parent d910b74 commit cc3acde
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/nrnpython/nrnpy_hoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2652,10 +2652,8 @@ extern "C" NRN_EXPORT int nrnpy_set_gui_callback(PyObject* new_gui_callback) {
}

static double object_to_double_(Object* obj) {
PyObject* const pyobj = nrnpy_ho2po(obj);
Py_INCREF(pyobj);
const double result = PyFloat_AsDouble(pyobj);
Py_DECREF(pyobj);
auto pyobj = nb::steal(nrnpy_ho2po(obj));
const double result = PyFloat_AsDouble(pyobj.ptr());
return result;
}

Expand Down

0 comments on commit cc3acde

Please sign in to comment.