Merge pull request #28085 from Ghazi-raad:fix/multiband-blender-memory-leak-27333

Fixed multiband blender memory leak 27333 #28085

Fixes #27333

This PR fixes a memory leak in MultiBandBlender where memory from pyramid vectors was not being released when prepare() was called multiple times or when the blender object was reused.

Problem:

MultiBandBlender retains hundreds of MB to several GB of memory even after the blender pointer is released. The issue occurs because:

1. The resize() function on std::vector does not release memory when the new size is less than or equal to the current size
2. It only adjusts the size marker while retaining the capacity and existing data
3. When prepare() is called, the pyramid vectors are resized but old data remains allocated

Example from the bug report: Blending 14 images (1920x1080) retained ~200MB after blender.release(). With larger images, several GB could be retained.

Root Cause:

GPU path (lines 254-256): Correctly calls clear() before operations
Non-GPU path (lines 285-298): Missing clear() calls, causing resize() to retain old data

Solution:

Added .clear() calls before .resize() in the non-GPU path to match the GPU path behavior. This ensures:
- Memory from previous blend operations is released in prepare()
- Reusing a blender object doesn't accumulate memory
- Behavior is consistent between GPU and non-GPU code paths

Changes:

modules/stitching/src/blenders.cpp: Added 2 clear() calls (dst_pyr_laplace_.clear() and dst_band_weights_.clear()) before resize() in the non-GPU path

Pull Request Readiness Checklist:

- [x] I agree to contribute to the project under Apache 2 License.
- [x] 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
- [x] The PR is proposed to the proper branch (4.x)
- [x] There is a reference to the original bug report and related work (issue #27333)
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      N/A - This is a memory management fix that doesn't affect algorithm behavior. Existing stitching tests verify correctness.
- [x] The feature is well documented and sample code can be built with the project CMake
      The fix maintains existing API behavior, no documentation changes needed
This commit is contained in:
Ghazi-raad
2025-11-27 06:57:21 +00:00
committed by GitHub
parent 4da9a518ef
commit 71c759b7cd

View File

@@ -277,6 +277,9 @@ void MultiBandBlender::prepare(Rect dst_roi)
else
#endif
{
dst_pyr_laplace_.clear();
dst_band_weights_.clear();
dst_pyr_laplace_.resize(num_bands_ + 1);
dst_pyr_laplace_[0] = dst_;