From 723c4ae76d8c4123e614110da3c9ce22ad094f51 Mon Sep 17 00:00:00 2001 From: Jiabin Yang <360788950@qq.com> Date: Fri, 20 May 2022 16:08:26 +0800 Subject: [PATCH] fix hook mem leak (#42857) --- paddle/fluid/pybind/eager_method.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 4d7b80a4e8c..1a0838d7f47 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -77,7 +77,9 @@ class PyTensorHook : public egr::TensorHook { PyObject* res = nullptr; try { - res = PyObject_CallFunctionObjArgs(py_func_, ToPyObject(var), nullptr); + PyObject* p_tmp_var = ToPyObject(var); + res = PyObject_CallFunctionObjArgs(py_func_, p_tmp_var, nullptr); + Py_DECREF(p_tmp_var); } catch (platform::EnforceNotMet& e) { throw std::move(e); } catch (std::exception& e) { @@ -94,7 +96,9 @@ class PyTensorHook : public egr::TensorHook { if (res == Py_None) { return var; } - return reinterpret_cast(res)->tensor; + auto res_tensor = reinterpret_cast(res)->tensor; + Py_DECREF(res); + return res_tensor; } private: -- GitLab