Skip to content

Commit f9dc052

Browse files
AstroStrayStephenTheodoreclaude
authored
fix: Resolve IDisposable memory leaks in image source strategies (#36)
* fix: Resolve IDisposable memory leaks in image source strategies - Fix Mat object disposal in FileImageSourceStrategy when empty frames/images - Fix Mat object disposal in CameraImageSourceStrategy when empty frames - Add proper exception handling with disposal in GetImageFrameAsync - Prevents OpenCV Mat memory leaks identified by CodeQL analysis Resolves #35 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * fix: Add comprehensive exception handling for Mat disposal - Wrap frame operations in try-catch blocks to ensure disposal - Prevent memory leaks when Read() or Empty() methods throw exceptions - Address GitHub Advanced Security Bot recommendations - Improve robustness of OpenCV resource management 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Stephen Theodore <wssmash93@gmail.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 62e6e16 commit f9dc052

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

.claude/settings.local.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"Bash(gh pr:*)",
2222
"Bash(dotnet build:*)",
2323
"Bash(gh label:*)",
24-
"Bash(gh issue edit:*)"
24+
"Bash(gh issue edit:*)",
25+
"Bash(git commit:*)",
26+
"Bash(git push:*)"
2527
],
2628
"deny": [],
2729
"ask": []

src/MetaExtractor.Infrastructure/Services/CameraImageSourceStrategy.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,23 @@ private void InitializeCamera()
7878
public Task<Mat?> GetNextFrameAsync()
7979
{
8080
var frame = new Mat();
81-
_capture.Read(frame);
81+
try
82+
{
83+
_capture.Read(frame);
84+
85+
if (frame.Empty())
86+
{
87+
frame.Dispose();
88+
return Task.FromResult<Mat?>(null);
89+
}
8290

83-
if (frame.Empty())
91+
return Task.FromResult<Mat?>(frame);
92+
}
93+
catch
8494
{
95+
frame.Dispose();
8596
return Task.FromResult<Mat?>(null);
8697
}
87-
88-
return Task.FromResult<Mat?>(frame);
8998
}
9099

91100
public void Dispose()

src/MetaExtractor.Infrastructure/Services/FileImageSourceStrategy.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,23 @@ public FileImageSourceStrategy(string filePath)
5757
return Task.FromResult<Mat?>(null);
5858

5959
var frame = new Mat();
60-
_videoCapture.Read(frame);
60+
try
61+
{
62+
_videoCapture.Read(frame);
63+
64+
if (frame.Empty())
65+
{
66+
frame.Dispose();
67+
return Task.FromResult<Mat?>(null);
68+
}
6169

62-
if (frame.Empty())
70+
return Task.FromResult<Mat?>(frame);
71+
}
72+
catch
6373
{
74+
frame.Dispose();
6475
return Task.FromResult<Mat?>(null);
6576
}
66-
67-
return Task.FromResult<Mat?>(frame);
6877
}
6978

7079
private Task<Mat?> GetImageFrameAsync()
@@ -76,20 +85,23 @@ public FileImageSourceStrategy(string filePath)
7685
}
7786

7887
_isImageRead = true;
79-
88+
89+
Mat? mat = null;
8090
try
8191
{
82-
var mat = new Mat(_filePath, ImreadModes.Color);
92+
mat = new Mat(_filePath, ImreadModes.Color);
8393

8494
if (mat.Empty())
8595
{
96+
mat.Dispose();
8697
return Task.FromResult<Mat?>(null);
8798
}
8899

89100
return Task.FromResult<Mat?>(mat);
90101
}
91102
catch (Exception)
92103
{
104+
mat?.Dispose();
93105
return Task.FromResult<Mat?>(null);
94106
}
95107
}

0 commit comments

Comments
 (0)