Memory Leak in NWPathMonitor on iOS14

627 Views Asked by At

I just found a reproducible memory leak when using NWPathMonitor on iOS14. Also reproducible on iOS13, and I suspect 12 also, but have only been able to test iOS 13.

To reproduce the leak, all you have to do is allocate an instance of NWPathMonitor (no need to hold a reference to it), and as soon as you do, you'll see the leak appear when debugging in Instruments using the Leaks template, or using the Xcode memory graph. This is reproducible on the simulator and on a device.

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Just allocate the instance and immediately the leaks will show in instruments
        _ = NWPathMonitor()
        return true
    }
1

There are 1 best solutions below

0
On

Edit: I found this was my fault - I was forgetting to call NWPathMonitor's cancel() method in my class's deinit, which was the cause of the leak. Leaving this here in case anyone else finds it useful in future.

Original answer: Feedback opened with Apple and cross-posted to OpenRadar: https://openradar.appspot.com/radar?id=4969412406804480. Hopefully this will save someone else chasing their tail like I did, only to find it's a bug in the Network framework!