Cocoa/highgui: fix leak in cvGetWindowRect_COCOA#26625
Merged
asmorkalov merged 1 commit intoopencv:4.xfrom Dec 20, 2024
NekoAsakura:4.x
Merged
Cocoa/highgui: fix leak in cvGetWindowRect_COCOA#26625asmorkalov merged 1 commit intoopencv:4.xfrom NekoAsakura:4.x
asmorkalov merged 1 commit intoopencv:4.xfrom
NekoAsakura:4.x
Conversation
VadimLevin
requested changes
Dec 16, 2024
Contributor
There was a problem hiding this comment.
Tested on the following setup:
cpu: x86_64
macOS 15.2
Xcode 16.2
Requirements:
pip install --require-virtualenv psutil matplotlibCode for testing:
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
import psutil
NUM_ITERATIONS = 1000
def plot_memory_usage(memory_usage):
plt.figure(figsize=(10, 5))
x = np.arange(len(memory_usage))
plt.plot(x, memory_usage, label="Memory Usage (KB)", color="blue")
plt.title("Memory Usage Over Time")
plt.xlabel("Iterations")
plt.ylabel("Memory Usage (KB)")
plt.legend()
plt.grid()
plt.savefig("test_opencv_memory_leak.png")
def main():
process = psutil.Process(os.getpid())
shape = (800, 600, 3)
image = np.random.randint(0, 255 + 1, np.prod(shape), np.uint8).reshape(shape)
memory_usage = np.zeros(NUM_ITERATIONS, dtype=np.float32)
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
for i in range(NUM_ITERATIONS):
rect = cv2.getWindowImageRect("image")
cv2.imshow("image", image)
memory_usage[i] = process.memory_info().rss / 1024
if cv2.waitKey(1) == ord("q"):
break
print("Current memory:", memory_usage[-1], "KB")
print("Peak memory usage:", np.max(memory_usage), "KB")
plot_memory_usage(memory_usage)
if __name__ == "__main__":
main()Current memory: 2154040.0 KB
Peak memory usage: 3587704.0 KB
I think, the drop in memory usage corresponds to time when ARC kicks in.
Current memory: 92292.0 KB
Peak memory usage: 92292.0 KB
Contributor
Author
VadimLevin
approved these changes
Dec 20, 2024
6 tasks
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.



#26574 reported a memory leak occurring when calling
getWindowImageRecton macOS. After investigation, it was found that the leak was caused by thecvGetWindowRect_COCOA, which created several NSObject instances without setting up a local autorelease pool.opencv/modules/highgui/src/window_cocoa.mm
Lines 665 to 671 in 1d41108
The patch has been compiled and tested on macOS 15.1.1, confirming that the bug is resolved.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.