From a6e17f6360c8cbae99cc52ad11db51962d25de5d Mon Sep 17 00:00:00 2001 From: William Zijie Zhang Date: Mon, 9 Nov 2026 13:14:06 -0800 Subject: [PATCH] Fix leaked arrays in Jacobian and Hessian bindings Py_BuildValue's "O" adds a reference, so the arrays from PyArray_SimpleNew kept one extra and were never freed. "N" hands the new reference to the tuple. 20k problem_jacobian calls at n=2000 grew RSS by ~665 MB before; flat after. Co-Authored-By: Claude Opus 5.5 --- sparsediffpy/_bindings/problem/hessian.h | 6 +++--- sparsediffpy/_bindings/problem/jacobian.h | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sparsediffpy/_bindings/problem/hessian.h b/sparsediffpy/_bindings/problem/hessian.h index cbc6428..880e4c5 100644 --- a/sparsediffpy/_bindings/problem/hessian.h +++ b/sparsediffpy/_bindings/problem/hessian.h @@ -71,7 +71,7 @@ static PyObject *py_problem_hessian(PyObject *self, PyObject *args) memcpy(PyArray_DATA((PyArrayObject *) indices), H->i, nnz * sizeof(int)); memcpy(PyArray_DATA((PyArrayObject *) indptr), H->p, n_plus_1 * sizeof(int)); - return Py_BuildValue("(OOO(ii))", data, indices, indptr, H->m, H->n); + return Py_BuildValue("(NNN(ii))", data, indices, indptr, H->m, H->n); } static PyObject *py_get_hessian(PyObject *self, PyObject *args) @@ -117,7 +117,7 @@ static PyObject *py_get_hessian(PyObject *self, PyObject *args) memcpy(PyArray_DATA((PyArrayObject *) indices), H->i, nnz * sizeof(int)); memcpy(PyArray_DATA((PyArrayObject *) indptr), H->p, n_plus_1 * sizeof(int)); - return Py_BuildValue("(OOO(ii))", data, indices, indptr, H->m, H->n); + return Py_BuildValue("(NNN(ii))", data, indices, indptr, H->m, H->n); } static PyObject *py_get_problem_hessian_sparsity_coo(PyObject *self, PyObject *args) @@ -160,7 +160,7 @@ static PyObject *py_get_problem_hessian_sparsity_coo(PyObject *self, PyObject *a memcpy(PyArray_DATA((PyArrayObject *) rows), coo->rows, nnz * sizeof(int)); memcpy(PyArray_DATA((PyArrayObject *) cols), coo->cols, nnz * sizeof(int)); - return Py_BuildValue("(OO(ii))", rows, cols, coo->m, coo->n); + return Py_BuildValue("(NN(ii))", rows, cols, coo->m, coo->n); } static PyObject *py_problem_eval_hessian_vals_coo(PyObject *self, PyObject *args) diff --git a/sparsediffpy/_bindings/problem/jacobian.h b/sparsediffpy/_bindings/problem/jacobian.h index 73dc6f9..19027f5 100644 --- a/sparsediffpy/_bindings/problem/jacobian.h +++ b/sparsediffpy/_bindings/problem/jacobian.h @@ -28,7 +28,7 @@ static PyObject *py_problem_jacobian(PyObject *self, PyObject *args) PyObject *indices = PyArray_SimpleNew(1, &zero, NPY_INT32); PyObject *indptr = PyArray_SimpleNew(1, &one, NPY_INT32); ((int *) PyArray_DATA((PyArrayObject *) indptr))[0] = 0; - return Py_BuildValue("(OOO(ii))", data, indices, indptr, 0, prob->n_vars); + return Py_BuildValue("(NNN(ii))", data, indices, indptr, 0, prob->n_vars); } problem_jacobian(prob); @@ -53,7 +53,7 @@ static PyObject *py_problem_jacobian(PyObject *self, PyObject *args) memcpy(PyArray_DATA((PyArrayObject *) indices), jac->i, nnz * sizeof(int)); memcpy(PyArray_DATA((PyArrayObject *) indptr), jac->p, m_plus_1 * sizeof(int)); - return Py_BuildValue("(OOO(ii))", data, indices, indptr, jac->m, jac->n); + return Py_BuildValue("(NNN(ii))", data, indices, indptr, jac->m, jac->n); } static PyObject *py_get_jacobian(PyObject *self, PyObject *args) @@ -99,7 +99,7 @@ static PyObject *py_get_jacobian(PyObject *self, PyObject *args) memcpy(PyArray_DATA((PyArrayObject *) indices), jac->i, nnz * sizeof(int)); memcpy(PyArray_DATA((PyArrayObject *) indptr), jac->p, m_plus_1 * sizeof(int)); - return Py_BuildValue("(OOO(ii))", data, indices, indptr, jac->m, jac->n); + return Py_BuildValue("(NNN(ii))", data, indices, indptr, jac->m, jac->n); } static PyObject *py_get_jacobian_sparsity_coo(PyObject *self, PyObject *args) @@ -142,7 +142,7 @@ static PyObject *py_get_jacobian_sparsity_coo(PyObject *self, PyObject *args) memcpy(PyArray_DATA((PyArrayObject *) rows), coo->rows, nnz * sizeof(int)); memcpy(PyArray_DATA((PyArrayObject *) cols), coo->cols, nnz * sizeof(int)); - return Py_BuildValue("(OO(ii))", rows, cols, coo->m, coo->n); + return Py_BuildValue("(NN(ii))", rows, cols, coo->m, coo->n); } static PyObject *py_problem_eval_jacobian_vals(PyObject *self, PyObject *args)