Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/SystemConfiguration/NetworkReachability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,15 @@ public NetworkReachability (IPAddress localAddress, IPAddress remoteAddress)
{
}

protected override void Dispose (bool disposing)
{
if (gchAllocated) {
gch.Free ();
gchAllocated = false;
}
base.Dispose (disposing);
}

[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("tvos")]
[SupportedOSPlatform ("maccatalyst")]
Expand Down Expand Up @@ -487,6 +496,7 @@ public StatusCode GetFlags (out NetworkReachabilityFlags flags)

Notification? notification;
GCHandle gch;
bool gchAllocated;

[UnmanagedCallersOnly]
static void Callback (IntPtr handle, NetworkReachabilityFlags flags, IntPtr info)
Expand Down Expand Up @@ -518,6 +528,7 @@ public StatusCode SetNotification (Notification? callback)
return StatusCode.OK;

gch = GCHandle.Alloc (this);
gchAllocated = true;
var ctx = new SCNetworkReachabilityContext (GCHandle.ToIntPtr (gch));

unsafe {
Expand All @@ -532,6 +543,11 @@ public StatusCode SetNotification (Notification? callback)
if (!rv)
return StatusCodeError.SCError ();

if (gchAllocated) {
gch.Free ();
gchAllocated = false;
}

return StatusCode.OK;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,98 @@ public void Schedule ()
Assert.IsTrue (defaultRouteReachability.Schedule (CFRunLoop.Main, CFRunLoop.ModeDefault), "Schedule");
Assert.IsTrue (defaultRouteReachability.Unschedule (CFRunLoop.Main, CFRunLoop.ModeDefault), "Unschedule");
}

[Test]
public void SetNotification_SetAndRemove ()
{
using var nr = new NetworkReachability (IPAddress.Loopback);
bool callbackInvoked = false;
NetworkReachability.Notification callback = (flags) => {
callbackInvoked = true;
};

// Set notification
var result = nr.SetNotification (callback);
Assert.AreEqual (StatusCode.OK, result, "SetNotification should succeed");

// Remove notification
result = nr.SetNotification (null);
Assert.AreEqual (StatusCode.OK, result, "Removing notification should succeed");

// Setting null again should be OK
result = nr.SetNotification (null);
Assert.AreEqual (StatusCode.OK, result, "Setting null when no notification is set should succeed");
}

[Test]
public void SetNotification_DisposeAfterSet ()
{
// This test ensures that disposing after setting a notification doesn't leak the GCHandle
var nr = new NetworkReachability (IPAddress.Loopback);
bool callbackInvoked = false;
NetworkReachability.Notification callback = (flags) => {
callbackInvoked = true;
};

var result = nr.SetNotification (callback);
Assert.AreEqual (StatusCode.OK, result, "SetNotification should succeed");

// Dispose without removing notification - should not leak
nr.Dispose ();
}

[Test]
public void SetNotification_MultipleSetRemoveCycles ()
{
using var nr = new NetworkReachability (IPAddress.Loopback);
bool callbackInvoked = false;
NetworkReachability.Notification callback = (flags) => {
callbackInvoked = true;
};

// Set notification
var result = nr.SetNotification (callback);
Assert.AreEqual (StatusCode.OK, result, "First SetNotification should succeed");

// Remove notification
result = nr.SetNotification (null);
Assert.AreEqual (StatusCode.OK, result, "First remove should succeed");

// Set notification again
result = nr.SetNotification (callback);
Assert.AreEqual (StatusCode.OK, result, "Second SetNotification should succeed");

// Remove notification again
result = nr.SetNotification (null);
Assert.AreEqual (StatusCode.OK, result, "Second remove should succeed");
}

[Test]
public void SetNotification_UpdateCallback ()
{
using var nr = new NetworkReachability (IPAddress.Loopback);
bool firstCallbackInvoked = false;
bool secondCallbackInvoked = false;

NetworkReachability.Notification firstCallback = (flags) => {
firstCallbackInvoked = true;
};

NetworkReachability.Notification secondCallback = (flags) => {
secondCallbackInvoked = true;
};

// Set first notification
var result = nr.SetNotification (firstCallback);
Assert.AreEqual (StatusCode.OK, result, "First SetNotification should succeed");

// Update with second notification
result = nr.SetNotification (secondCallback);
Assert.AreEqual (StatusCode.OK, result, "Updating notification should succeed");

// Remove notification
result = nr.SetNotification (null);
Assert.AreEqual (StatusCode.OK, result, "Removing notification should succeed");
}
}
}
Loading