Network.Framework UDP Broadcast: Leak?

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Network.Framework UDP Broadcast: Leak?

    Folgend ein kleines UDP NWListener Beispiel als CommandLineApp

    Quellcode

    1. // Network.Framework UDP Broadcast Test
    2. // echo "hello hello" | socat - UDP-DATAGRAM:255.255.255.255:45678,broadcast
    3. /*
    4. while :
    5. do
    6. echo "hello" | socat - UDP-DATAGRAM:255.255.255.255:45678,broadcast
    7. sleep 0.333
    8. done
    9. */
    10. import Foundation
    11. import Network
    12. let listener = try NWListener(using: .udp, on: 45678)
    13. listener.stateUpdateHandler = { (state) -> Void in
    14. print("Listener state: \(state)")
    15. }
    16. listener.newConnectionHandler = { (conn) -> Void in
    17. conn.stateUpdateHandler = { (state) -> Void in
    18. print("Connection state: \(state)")
    19. }
    20. conn.receiveMessage() { (data, context, isComplete, error) in
    21. if let error = error {
    22. print("Error: \(error)")
    23. } else if let data = data {
    24. guard let str = String(data: data, encoding: .utf8) else {
    25. print("Failed to parse string!")
    26. return
    27. }
    28. print(str)
    29. }
    30. }
    31. conn.start(queue: .main)
    32. }
    33. listener.start(queue: .main)
    34. dispatchMain()
    Alles anzeigen
    Irgendwie verstehe ich nicht, wann die NWConnection conn aufgeräumt wird? Habe die Vermutung, dass das gar nicht passiert. Also dass da ein Leak drinnen ist. Das Ergebnis vom Profiler verstehe ich auch nicht so ganz im Moment. Weiß das wer?
  • Im Network.Framework Header-File steht zu cancel eine etwas andere Beschreibung

    Header schrieb:

    /// Cancel the connection, and cause all update handlers to be cancelled.

    ///
    /// Cancel is asynchronous. The last callback will be to the stateUpdateHandler with the cancelled state. After
    /// the stateUpdateHandlers are called with the cancelled state, all blocks are released to break retain cycles.
    ///
    /// Calls to cancel after the first one are ignored.
    final public func cancel()
    als einfach nur

    Docu schrieb:

    Cancels the connection and gracefully disconnects any established network protocols.
    Wenn man das jetzt mit einbaut, dann markiert Instruments nichts mehr als Leak. Ist zwar iwie. lustig, wenn etwas fertig ist, dass man das noch "canceln" muss, um ein "clean up" zu machen, Aber dann ist es halt so.