From c46f2ddb55ea075226e144b0222a38eba5352f53 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Sat, 19 Mar 2022 13:05:01 +0800 Subject: [PATCH] fix python hook mem leak (#40716) --- paddle/fluid/pybind/imperative.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/pybind/imperative.cc b/paddle/fluid/pybind/imperative.cc index 85427a8455b..3a2c93309f3 100644 --- a/paddle/fluid/pybind/imperative.cc +++ b/paddle/fluid/pybind/imperative.cc @@ -119,7 +119,11 @@ class PyVariableWrapperHook : public imperative::VariableWrapperHook { return var; } - return PyObjectCast>(res)->SharedVar(); + auto res_varbase = PyObjectCast>(res); + // Here the reference count of `res` is 2, so we decreases the reference + // count manually to avoid memory leaks + Py_DECREF(res); + return res_varbase->SharedVar(); } private: -- GitLab