Fix memory leak in pyopencv_to for path-like objects#28047
Merged
asmorkalov merged 3 commits intoopencv:4.xfrom Nov 26, 2025
Merged
Fix memory leak in pyopencv_to for path-like objects#28047asmorkalov merged 3 commits intoopencv:4.xfrom
asmorkalov merged 3 commits intoopencv:4.xfrom
Conversation
PyOS_FSPath returns a new strong reference that must be decremented. Added proper reference counting by storing the returned value and calling Py_XDECREF on all exit paths. Fixes opencv#28046
asmorkalov
reviewed
Nov 20, 2025
Contributor
Author
|
Thanks for fixing the scope issue! I see you've pushed the corrections directly - appreciate you taking the time to clean that up. I'll make sure to be more careful with preprocessor blocks in future contributions. |
Contributor
Author
|
It looks like the Windows x64 test failed due to a network error (ECONNRESET) rather than a code issue. The other CI checks have passed. Could someone restart this check when possible? |
asmorkalov
approved these changes
Nov 26, 2025
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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