Skip to content

fix: dispose ManagementObject in MemoryTestService and SkiaSharp paints in NetworkSharedState - #441

Merged
laurentiu021 merged 1 commit into
mainfrom
fix/resource-leaks
May 19, 2026
Merged

laurentiu021 merged 1 commit into
mainfrom
fix/resource-leaks

Conversation

@laurentiu021

@laurentiu021 laurentiu021 commented May 19, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fix two resource leaks identified in the full code review (LEAK-003, LEAK-007 subset).

Changes

MemoryTestService.cs

  • Wrap ManagementObject mo in using (mo) inside the GetModulesAsync foreach loop.
    Without this, each WMI object's native COM handle leaks until GC finalizer runs.

NetworkSharedState.cs

  • Dispose() now disposes all SkiaSharp paint resources:
    • Series paints (Stroke, GeometryStroke, GeometryFill, Fill) for both latency and trace series
    • Axis paints (NamePaint, LabelsPaint, SeparatorsPaint) for all 4 axis arrays
    • Class-level paints (LegendTextPaint, LegendBackgroundPaint, TooltipTextPaint, TooltipBackgroundPaint)
  • DisposeSeries now handles both LineSeries<DateTimePoint> and LineSeries<ObservablePoint>
  • Added DisposeAxisPaints helper method

Previously fixed (verified, no action needed)

  • LEAK-001 (BatteryService, DiskHealthService, FixedDriveService, SystemInfoService) — already use using (mo)
  • LEAK-002 (ShortcutCleanerService double ReleaseComObject) — already fixed with comment
  • LEAK-004 (UninstallerViewModel lambda) — already unsubscribes in Dispose
  • LEAK-005 (PowerShellRunner) — class has no persistent resources (creates Runspace per-call with using)
  • LEAK-006 (TrayIconService stream) — already uses using var stream
  • LEAK-007 (MemoryTestService Process.Start) — already uses using var proc

Testing

  • Build: 0 errors (main project + test project)
  • Existing tests unaffected (no behavior change, only resource cleanup)

@coderabbitai

coderabbitai Bot commented May 19, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR fixes two unmanaged resource leaks: WMI ManagementObject instances in physical memory enumeration now dispose per-item via using blocks, and NetworkSharedState chart cleanup is expanded to fully dispose SkiaSharp SKPaint objects for series, axes, and legend/tooltip components instead of only typefaces. Changes are documented in the CHANGELOG.

Changes

Resource Leak Fixes

Layer / File(s) Summary
WMI ManagementObject per-item disposal
SysManager/SysManager/Services/MemoryTestService.cs
GetModulesAsync wraps each WMI ManagementObject iteration in a using (mo) block to ensure native handle release before building the corresponding health object.
SkiaSharp comprehensive resource cleanup
SysManager/SysManager/ViewModels/NetworkSharedState.cs
DisposeSeries now includes trace series paint disposal, and Dispose() is extended to dispose all series paints, axis paints (via new DisposeAxisPaints helper), and legend/tooltip paint objects with typefaces.
Changelog documentation
CHANGELOG.md
Two new [Unreleased] → Fixed entries document the MemoryTestService and NetworkSharedState resource leak fixes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 A leak in the wild, so subtle and deep,
Where objects and paints couldn't sleep,
But now with using blocks and care,
Each resource released without a tear,
The rabbit hops on, no handles to keep! 🎨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title precisely summarizes the two main changes: disposing ManagementObject in MemoryTestService and disposing SkiaSharp paints in NetworkSharedState, matching the core fix objectives.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/resource-leaks

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
SysManager/SysManager/Services/MemoryTestService.cs (1)

102-105: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Dispose the ManagementObjectCollection returned by s.Get()

Line 104 enumerates s.Get() directly without disposing the returned collection, which is a resource leak. Other files in the codebase (SystemInfoService.cs, DiskHealthService.cs, BatteryService.cs, FixedDriveService.cs) correctly wrap this call with using var, but MemoryTestService.cs fails to do so.

Wrap the result in a using statement before the foreach loop:

Proposed fix
                using var s = new ManagementObjectSearcher(
                    "SELECT BankLabel, DeviceLocator, Manufacturer, Capacity, Speed, ConfiguredClockSpeed, PartNumber FROM Win32_PhysicalMemory");
-                foreach (ManagementObject mo in s.Get())
+                using var modules = s.Get();
+                foreach (ManagementObject mo in modules)
                 {
                     using (mo)
                     {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@SysManager/SysManager/Services/MemoryTestService.cs` around lines 102 - 105,
The ManagementObjectCollection returned by ManagementObjectSearcher.Get() in
MemoryTestService (the call s.Get() used in the foreach) must be disposed to
avoid a resource leak; change the foreach to first capture the collection in a
using (e.g. using var collection = s.Get()) and then iterate over that
collection (foreach (ManagementObject mo in collection)), mirroring the pattern
used in SystemInfoService.cs and others so the ManagementObjectCollection is
properly disposed.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@SysManager/SysManager/Services/MemoryTestService.cs`:
- Around line 102-105: The ManagementObjectCollection returned by
ManagementObjectSearcher.Get() in MemoryTestService (the call s.Get() used in
the foreach) must be disposed to avoid a resource leak; change the foreach to
first capture the collection in a using (e.g. using var collection = s.Get())
and then iterate over that collection (foreach (ManagementObject mo in
collection)), mirroring the pattern used in SystemInfoService.cs and others so
the ManagementObjectCollection is properly disposed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f7d57dc9-4112-4e77-930c-9a454f4ec398

📥 Commits

Reviewing files that changed from the base of the PR and between 624462c and 9d02859.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • SysManager/SysManager/Services/MemoryTestService.cs
  • SysManager/SysManager/ViewModels/NetworkSharedState.cs
📜 Review details
🔇 Additional comments (2)
SysManager/SysManager/ViewModels/NetworkSharedState.cs (1)

263-269: LGTM!

Also applies to: 561-587

CHANGELOG.md (1)

9-16: LGTM!

@laurentiu021
laurentiu021 merged commit 98952ae into main May 19, 2026
5 checks passed
@laurentiu021
laurentiu021 deleted the fix/resource-leaks branch May 19, 2026 14:30
laurentiu021 added a commit that referenced this pull request May 22, 2026
Adds CHANGELOG entry for v0.28.26 — CodeQL regression fixes
(missed-where + missed-using).

Co-authored-by: laurentiu021 <laurentiu021@users.noreply.github.com>
laurentiu021 added a commit that referenced this pull request May 22, 2026
Adds CHANGELOG entry for v0.28.26 — CodeQL regression fixes
(missed-where + missed-using).

Co-authored-by: laurentiu021 <laurentiu021@users.noreply.github.com>
laurentiu021 added a commit that referenced this pull request May 22, 2026
…ts in NetworkSharedState (#441)

Co-authored-by: laurentiu021 <laurentiu021@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant