From df47d09929ef4f13e94e337da165d1f1f113c0ba Mon Sep 17 00:00:00 2001 From: MeeseeksMachine <39504233+meeseeksmachine@users.noreply.github.com> Date: Sat, 22 Jan 2022 01:04:11 -0800 Subject: [PATCH] Backport PR #45489: Bug tojson memleak (#45545) Co-authored-by: vernetya <52132110+vernetya@users.noreply.github.com> --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/_libs/src/ujson/python/objToJSON.c | 1 + 2 files changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 327432de0a3f7..363d4b57544a9 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -966,6 +966,7 @@ I/O - Bug in :func:`read_json` raising ``ValueError`` when attempting to parse json strings containing "://" (:issue:`36271`) - Bug in :func:`read_csv` when ``engine="c"`` and ``encoding_errors=None`` which caused a segfault (:issue:`45180`) - Bug in :func:`read_csv` an invalid value of ``usecols`` leading to an unclosed file handle (:issue:`45384`) +- Bug in :meth:`DataFrame.to_json` fix memory leak (:issue:`43877`) Period ^^^^^^ diff --git a/pandas/_libs/src/ujson/python/objToJSON.c b/pandas/_libs/src/ujson/python/objToJSON.c index 06c3d823f72d2..c4609992342c3 100644 --- a/pandas/_libs/src/ujson/python/objToJSON.c +++ b/pandas/_libs/src/ujson/python/objToJSON.c @@ -228,6 +228,7 @@ static PyObject *get_values(PyObject *obj) { PyErr_Clear(); } else if (PyObject_HasAttrString(values, "__array__")) { // We may have gotten a Categorical or Sparse array so call np.array + Py_DECREF(values); values = PyObject_CallMethod(values, "__array__", NULL); } else if (!PyArray_CheckExact(values)) { // Didn't get a numpy array, so keep trying