Hi,
I’m using Network Framework to implement a UDP client via NWConnection, and I’m looking for clarification about the correct and fully safe shutdown procedure, especially regarding resource release.
I have initiated some pending receive calls on the NWConnection (using receive). After calling connection.cancel(), do we need to wait for the cancellation of these pending receives?
As mentioned in this thread, NWConnection retains references to the receive closures and releases them once they are called. If a receive closure holds a reference to the NWConnection itself, do we need to wait for these closures to be called to avoid memory leaks? Or, if there are no such retained references, we don't need to wait for the cancellation of the pending I/O and cancelled state for NWConnection?
do we need to wait for the cancellation of these pending receives?
This is very much like my answer on your other thread about listeners: The system doesn’t care, but there might be reasons for you to care.
The connection will hold a strong reference to your receive closure until it calls it. After that, it will release that reference. If the closure references the connection, which is very common, then that forms a retain loop. However, that’s not a problem because, in general, you shouldn’t rely on releasing the last reference to cancel the connection. Rather, you should call cancel() on it. That’s guaranteed to complete all the outstanding receives which breaks any retain loops they might’ve formed.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"