From 166f4591d216af0a86b73f3841231975e5625389 Mon Sep 17 00:00:00 2001 From: Anshu Date: Wed, 26 Nov 2025 14:48:42 +0530 Subject: [PATCH] Merge pull request #28047 from 0AnshuAditya0:fix-pyos-fspath-memory-leak MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix memory leak in pyopencv_to for path-like objects #28047 This PR fixes a memory leak in pyopencv_to when handling path-like objects (e.g., pathlib.Path). Problem: PyOS_FSPath() returns a new strong reference, but the code was not calling Py_XDECREF to decrement it, causing a memory leak on every call with path-like arguments. Solution: Store the returned reference from PyOS_FSPath() in a separate variable path_obj Call Py_XDECREF(path_obj) on all function exit paths (both success and error paths) This ensures proper reference counting without changing the function's behavior Testing: The leak can be reproduced using the steps in issue #28046 with Python built with --with-address-sanitizer. This fix ensures the reference is properly released. Fixes #28046 Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request ✓ I agree to contribute to the project under Apache 2 License. ✓ To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV ✓ The PR is proposed to the proper branch (4.x) ✓ There is a reference to the original bug report and related work (#28046) NA There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. ✓ The feature is well documented and sample code can be built with the project CMake --- modules/python/src2/cv2_convert.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/modules/python/src2/cv2_convert.cpp b/modules/python/src2/cv2_convert.cpp index 577c079dcd..1161995495 100644 --- a/modules/python/src2/cv2_convert.cpp +++ b/modules/python/src2/cv2_convert.cpp @@ -711,28 +711,29 @@ bool pyopencv_to(PyObject* obj, String &value, const ArgInfo& info) std::string str; #if ((PY_VERSION_HEX >= 0x03060000) && !defined(Py_LIMITED_API)) || (Py_LIMITED_API >= 0x03060000) + PyObject* path_obj = NULL; if (info.pathlike) { - obj = PyOS_FSPath(obj); + path_obj = PyOS_FSPath(obj); if (PyErr_Occurred()) { failmsg("Expected '%s' to be a str or path-like object", info.name); return false; } + obj = path_obj; } #endif + + bool result = false; if (getUnicodeString(obj, str)) { value = str; - return true; + result = true; } else { - // If error hasn't been already set by Python conversion functions if (!PyErr_Occurred()) { - // Direct access to underlying slots of PyObjectType is not allowed - // when limited API is enabled #ifdef Py_LIMITED_API failmsg("Can't convert object to 'str' for '%s'", info.name); #else @@ -741,7 +742,12 @@ bool pyopencv_to(PyObject* obj, String &value, const ArgInfo& info) #endif } } - return false; + +#if ((PY_VERSION_HEX >= 0x03060000) && !defined(Py_LIMITED_API)) || (Py_LIMITED_API >= 0x03060000) + Py_XDECREF(path_obj); +#endif + + return result; } template<>