Once started, NWPathMonitor appears to be kept alive until cancelled, but is this documented?

NWPathMonitor appears to retain itself (or is retained by some internal infrastructure) once it has been started until cancelled. This seems like it can lead to memory leaks if the references to to the monitor are dropped. Is this behavior documented anywhere?

func nwpm_self_retain() {
    weak var weakRef: NWPathMonitor?
    autoreleasepool {
        let monitor: NWPathMonitor = NWPathMonitor()
        weakRef = monitor
        monitor.start(queue: .main)
        // monitor.cancel() // assertion fails unless this is called
    }

    assert(weakRef == nil)
}
nwpm_self_retain()

NWPathMonitor appears to retain itself (or is retained by some internal infrastructure) once it has been started until cancelled.

Basically, yes. Start is registering a handler that's receiving events from the system, which, conceptually, ends up retaining the NWPathMonitor so that the handler has a valid target.

This seems like it can lead to memory leaks if the references to the monitor are dropped.

I haven't specifically tested it, but yes, that's certainly possible. You should cancel every NWPathMonitor that you start.

Having said that, it's also possible that your test isn't entirely valid. The "start()" process is an asynchronous action, so it's possible NWPathMonitor is temporarily holding a reference which it will drop once start later completes. If you wanted to test this properly, then you'd need to set a cancellation handler and see if that was called.

Is this behavior documented anywhere?

I'm not sure. I suspect it was documented when the API was initially released, but the network framework has gone through multiple revisions of its Swift binding, and it's possible this detail got dropped during that. However, it's not mentioned in the header doc, so it's possible it was never formally documented.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Once started, NWPathMonitor appears to be kept alive until cancelled, but is this documented?
 
 
Q