(crash) NWPathMonitor EXC_BAD_ACCESS (code=1, address=0x0)

plateform: iPadOS 16.3.1

xcode:15.2

code:

 self.queue =  Queue()
        self.monitor = NWPathMonitor()
        
        self.monitor.pathUpdateHandler = { [weak self] path in
               queue.async {

                }
          }
Answered by DTS Engineer in 824439022

I’m not sure what’s going on here but the code you posted doesn’t make a lot of sense. When you use NWPathMonitor you have to call start(queue:) on it, which allows you to specify the queue on which it’s supposed to run. So, if you have an existing queue, you don’t need to bounce to that queue by an async(…) call. You can just tell Network framework to call your state update handler on that queue.

Here’s a minimal example:

import Network

func main() throws {
    let m = NWPathMonitor()
    m.pathUpdateHandler = { newPath in
        print(newPath)
    }
    m.start(queue: .main)
    withExtendedLifetime(m) {
        dispatchMain()
    }
}

try main()

IMPORTANT With is code for a macOS command-line tool. In iOS you wouldn’t use dispatchMain(). Moreover, the withExtendedLifetime(…) is only necessary to ensure that m stays around in memory. If you assign it to an object property, like you did in your snippet, you don’t need that either.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I’m not sure what’s going on here but the code you posted doesn’t make a lot of sense. When you use NWPathMonitor you have to call start(queue:) on it, which allows you to specify the queue on which it’s supposed to run. So, if you have an existing queue, you don’t need to bounce to that queue by an async(…) call. You can just tell Network framework to call your state update handler on that queue.

Here’s a minimal example:

import Network

func main() throws {
    let m = NWPathMonitor()
    m.pathUpdateHandler = { newPath in
        print(newPath)
    }
    m.start(queue: .main)
    withExtendedLifetime(m) {
        dispatchMain()
    }
}

try main()

IMPORTANT With is code for a macOS command-line tool. In iOS you wouldn’t use dispatchMain(). Moreover, the withExtendedLifetime(…) is only necessary to ensure that m stays around in memory. If you assign it to an object property, like you did in your snippet, you don’t need that either.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

(crash) NWPathMonitor EXC_BAD_ACCESS (code=1, address=0x0)
 
 
Q