Skip to content

Commit f66fc82

Browse files
committed
Explicitly decrement ref count in set_callback
1 parent f488d92 commit f66fc82

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

system-configuration/src/network_reachability.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -247,29 +247,32 @@ impl SCNetworkReachability {
247247

248248
let mut callback_context = SCNetworkReachabilityContext {
249249
version: 0,
250-
info: Arc::as_ptr(&callback) as *mut _,
250+
info: Arc::into_raw(callback) as *mut _,
251251
retain: Some(NetworkReachabilityCallbackContext::<F>::retain_context),
252252
release: Some(NetworkReachabilityCallbackContext::<F>::release_context),
253253
copyDescription: Some(NetworkReachabilityCallbackContext::<F>::copy_ctx_description),
254254
};
255255

256-
if unsafe {
257-
// The call to SCNetworkReachabilitySetCallback will call the
258-
// `retain` callback which will increment the reference count on
259-
// `callback`. Therefore, although `callback` is dropped when this
260-
// function goes out of scope, the reference count will still be >0.
261-
//
262-
// When `SCNetworkReachability` is dropped, `release` is called
263-
// which will drop the reference count on `callback` to 0.
264-
//
265-
// Assumes the pointer pointed to by the `info` member of `callback_context` is still valid.
256+
let result = unsafe {
266257
SCNetworkReachabilitySetCallback(
267258
self.0,
268259
Some(NetworkReachabilityCallbackContext::<F>::callback),
269260
&mut callback_context,
270261
)
271-
} == 0u8
272-
{
262+
};
263+
264+
// The call to SCNetworkReachabilitySetCallback will call the
265+
// `retain` callback which will increment the reference count on
266+
// `callback`. Therefore, although the count is decremented below,
267+
// the reference count will still be >0.
268+
//
269+
// When `SCNetworkReachability` is dropped, `release` is called
270+
// which will drop the reference count on `callback` to 0.
271+
//
272+
// Assumes the pointer pointed to by the `info` member of `callback_context` is still valid.
273+
unsafe { Arc::decrement_strong_count(callback_context.info) };
274+
275+
if result == 0u8 {
273276
Err(SetCallbackError {})
274277
} else {
275278
Ok(())

0 commit comments

Comments
 (0)