Merge pull request #28047 from 0AnshuAditya0:fix-pyos-fspath-memory-leak

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
This commit is contained in:
Anshu
2025-11-26 14:48:42 +05:30
committed by GitHub
parent 621ad482d6
commit 166f4591d2

View File

@@ -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<>