Skip to content

Fix memory leaks for JpegXLDecoder#26787

Merged
asmorkalov merged 1 commit intoopencv:4.xfrom
MaximSmolskiy:fix_memory_leaks_for_JpegXLDecoder
Jan 20, 2025
Merged

Fix memory leaks for JpegXLDecoder#26787
asmorkalov merged 1 commit intoopencv:4.xfrom
MaximSmolskiy:fix_memory_leaks_for_JpegXLDecoder

Conversation

@MaximSmolskiy
Copy link
Copy Markdown
Contributor

@MaximSmolskiy MaximSmolskiy commented Jan 16, 2025

Pull Request Readiness Checklist

Fix #26766

It should additionally fix issue itself (problem with remove on Windows), but I can't be completely sure - I can't test this on Windows

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
  • There is a reference to the original bug report and related work
  • 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

@Burnside999
Copy link
Copy Markdown
Contributor

I can't test this on Windows

@MaximSmolskiy I have tested it under Windows using MSVC and there should be no memory leak anymore.

But I just did a very basic test, I executed the imread function 100 times to read the jxl file, and the memory usage gradually went up to 659M before this PR, and after this PR, the memory usage was only 5M.

@sturkmen72
Copy link
Copy Markdown
Contributor

sturkmen72 commented Jan 17, 2025

i confirm the test pass on windows

frankly i did not understand what is the difference calling release() or reset()

    pointer release() noexcept {
        return _STD exchange(_Mypair._Myval2, nullptr);
    }

    void reset(pointer _Ptr = nullptr) noexcept {
        pointer _Old = _STD exchange(_Mypair._Myval2, _Ptr);
        if (_Old) {
            _Mypair._Get_first()(_Old);
        }
    }

@Burnside999
Copy link
Copy Markdown
Contributor

frankly i did not understand what is the difference calling release() or reset()

As I understand it, reset() releases the original resource managed by unique_ptr and initializes unique_ptr with a new pointer (nullptr by default). release() only releases ownership of the resource, returns the raw pointer to the caller, and then sets unique_ptr to nullptr.

When release() is called without saving its return value, the sole reference to the internal resource (the raw pointer) is lost, making the memory inaccessible and resulting in a memory leak.

@sturkmen72
Copy link
Copy Markdown
Contributor

I think I understand, probably
_Old is FILE * and _Mypair._Get_first() is fclose so the file closed.

        if (_Old) {
            _Mypair._Get_first()(_Old);
        }

@Burnside999
Copy link
Copy Markdown
Contributor

Burnside999 commented Jan 17, 2025

Yes. The first element of _Mypair is a deleter and the second element is raw pointer.

When JpegXLDecoder is initialized, the deleter for m_f is set to fclose, so it can then be deleted correctly with reset().

@asmorkalov asmorkalov self-assigned this Jan 20, 2025
@asmorkalov asmorkalov added this to the 4.12.0 milestone Jan 20, 2025
@asmorkalov asmorkalov merged commit 2db6b29 into opencv:4.x Jan 20, 2025
@MaximSmolskiy MaximSmolskiy deleted the fix_memory_leaks_for_JpegXLDecoder branch January 20, 2025 10:33
@asmorkalov asmorkalov mentioned this pull request Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

remove(tmp_fname.c_str()) fails in test_jpegxl.cpp

4 participants