From efa74c491f5e60a86cf35b59b28540f2e56ceedd Mon Sep 17 00:00:00 2001 From: Joris Vaillant Date: Thu, 9 Nov 2023 16:13:06 +0100 Subject: [PATCH] binding: Fix memory leak in buildGeomFromUrdf and buildGeomFromUrdfString --- bindings/python/parsers/urdf/geometry.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/bindings/python/parsers/urdf/geometry.cpp b/bindings/python/parsers/urdf/geometry.cpp index 0485468b81..75dcd5c827 100644 --- a/bindings/python/parsers/urdf/geometry.cpp +++ b/bindings/python/parsers/urdf/geometry.cpp @@ -139,10 +139,20 @@ namespace pinocchio template static PyObject* postcall(ArgumentPackage const& args_, PyObject* result) { + // If owner_arg exist, we run bp::return_internal_reference postcall + // result lifetime will be tied to owner_arg lifetime PyObject* patient = bp::detail::get_prev::execute(args_, result); if (patient != Py_None) return bp::return_internal_reference::postcall(args_, result); - return result; + // If owner_arg doesn't exist, then Python will have to manage the result lifetime + bp::extract geom_model_extract(result); + if (geom_model_extract.check()) + { + return bp::to_python_indirect()(geom_model_extract()); + } + // If returned value is not a GeometryModel*, then raise an error + PyErr_SetString(PyExc_RuntimeError, "pinocchio::python::return_value_policy only work on GeometryModel* data type"); + return 0; } };