Skip to content

[UIKit] Fix GCHandle memory leak in NetworkReachability.SetNotification. Fixes #12962.#24147

Merged
rolfbjarne merged 5 commits intomainfrom
dev/rolf/fix-networkreachability-gchandle-leak
Nov 4, 2025
Merged

[UIKit] Fix GCHandle memory leak in NetworkReachability.SetNotification. Fixes #12962.#24147
rolfbjarne merged 5 commits intomainfrom
dev/rolf/fix-networkreachability-gchandle-leak

Conversation

@rolfbjarne
Copy link
Copy Markdown
Member

  • Free GCHandle when callback is removed via SetNotification(null)
  • Free GCHandle if SCNetworkReachabilitySetCallback fails
  • Override Dispose to ensure GCHandle is always freed
  • Add tests.

Mostly implemented using command-line Copilot.

Fixes #12962.

- Free GCHandle when callback is removed via SetNotification(null)
- Free GCHandle if SCNetworkReachabilitySetCallback fails
- Override Dispose to ensure GCHandle is always freed

Fixes #12962
Test verifies that SetNotification can be called multiple times
and that clearing the notification works properly.

Related to #12962
Add SetNotification_GCHandleFreed test that:
- Creates 10 NetworkReachability instances on a background thread
- Sets notifications to allocate GCHandles
- Disposes the instances to free the GCHandles
- Waits for background thread on main thread
- Forces garbage collection
- Asserts that at least one instance was collected by GC

This test verifies that the GCHandle leak fix works correctly and
objects can be garbage collected after disposal.
Add SetNotification_GCHandleFreedWithNull test that:
- Creates 10 NetworkReachability instances on a background thread
- Sets notifications to allocate GCHandles
- Calls SetNotification(null) to clear and free the GCHandles
- Waits for background thread on main thread
- Forces garbage collection
- Asserts that at least one instance was collected by GC

This test specifically validates that calling SetNotification(null)
properly frees the GCHandle, allowing objects to be garbage collected.
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #85b600d] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 85b600d7d623b0f3e6a172caabd0e5d2766fa543 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [PR Build #85b600d] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: 85b600d7d623b0f3e6a172caabd0e5d2766fa543 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 85b600d7d623b0f3e6a172caabd0e5d2766fa543 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

✅ [CI Build #85b600d] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 85b600d7d623b0f3e6a172caabd0e5d2766fa543 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

💻 [CI Build #85b600d] Tests on macOS X64 - Mac Sonoma (14) passed 💻

All tests on macOS X64 - Mac Sonoma (14) passed.

Pipeline on Agent
Hash: 85b600d7d623b0f3e6a172caabd0e5d2766fa543 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

💻 [CI Build #85b600d] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: 85b600d7d623b0f3e6a172caabd0e5d2766fa543 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

💻 [CI Build #85b600d] Tests on macOS M1 - Mac Ventura (13) passed 💻

All tests on macOS M1 - Mac Ventura (13) passed.

Pipeline on Agent
Hash: 85b600d7d623b0f3e6a172caabd0e5d2766fa543 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

💻 [CI Build #85b600d] Tests on macOS arm64 - Mac Sequoia (15) passed 💻

All tests on macOS arm64 - Mac Sequoia (15) passed.

Pipeline on Agent
Hash: 85b600d7d623b0f3e6a172caabd0e5d2766fa543 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne enabled auto-merge (squash) November 3, 2025 15:51
@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

💻 [CI Build #85b600d] Tests on macOS arm64 - Mac Tahoe (26) passed 💻

All tests on macOS arm64 - Mac Tahoe (26) passed.

Pipeline on Agent
Hash: 85b600d7d623b0f3e6a172caabd0e5d2766fa543 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Copy Markdown
Collaborator

🚀 [CI Build #85b600d] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 120 tests passed 🎉

Tests counts

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. [attempt 2] Html Report (VSDrops) Download
✅ linker: All 44 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 8 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 12 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 8 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. [attempt 3] Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 85b600d7d623b0f3e6a172caabd0e5d2766fa543 [PR build]

@rolfbjarne rolfbjarne merged commit 36c9009 into main Nov 4, 2025
45 checks passed
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.

NetworkReachability.SetNotification leaks a GCHandle

3 participants