Files
opencv/modules/python/src2
Anshu 166f4591d2 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
2025-11-26 12:18:42 +03:00
..