Reachability.swift icon indicating copy to clipboard operation
Reachability.swift copied to clipboard

Memory leak with startNotifier()

Open AlekseiR opened this issue 6 years ago • 4 comments

Provided with your code example I was able to use Reachability for a while now, but recently I've found that there is a leak which points to "try reachability.startNotifier()" using Debug Memory Graph.

class ConnectionManager: NSObject {
    static let sharedInstance = ConnectionManager()
    let reachability = Reachability()!
    let network = NetworkLoader()
    
    func observeReachability() {
        NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged), name: NSNotification.Name.reachabilityChanged, object: reachability)
        do {
            try reachability.startNotifier()
        } catch(let error) {
            print("Error occured while starting reachability notifications : \(error.localizedDescription)")
        }
    }
    
    func stopReachability() {
        reachability.stopNotifier()
        NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
    }
    
    @objc func reachabilityChanged(note: Notification) {
        guard let reachability = note.object as? Reachability else { return }
        switch reachability.connection {
        case .cellular:
            self.network.hideNetworkAlert()
            print("Network available via Cellular Data.")
        case .wifi:
            self.network.hideNetworkAlert()
            print("Network available via WiFi.")
            
        case .none:
            self.network.showNetworkAlert(reachability: self.reachability)
            print("Network is not available.")
        }
    }
}
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        ConnectionManager.sharedInstance.observeReachability()
        return true
    }
}

What version of Reachability.Swift are you using? 4.3.1 How is it installed? Manually, CocoaPods, Carthage? CocoaPods Does it occur on device or simulator? Which device? Both, physical device - iPhone XS What OS version are you running on? 12.1.3 Are there steps you can take to reproduce it?

  • Run the code
  • Use Debug Memory Graph

AlekseiR avatar May 14 '19 21:05 AlekseiR

Oddly I first observed the very same behavior. I also put Reachability into a Singleton, just like you. After a while I realized, that I just forgot NotificationCenter.default.removeObserver on deinit inside a Super Class I use.

joeypx avatar Jun 03 '19 09:06 joeypx

Oddly I first observed the very same behavior. I also put Reachability into a Singleton, just like you. After a while I realized, that I just forgot NotificationCenter.default.removeObserver on deinit inside a Super Class I use.

Well, exactly the same happens when using closures, so I suppose this doesn't relate to clearing observers.

AlekseiR avatar Jun 03 '19 14:06 AlekseiR

Oddly I first observed the very same behavior. I also put Reachability into a Singleton, just like you. After a while I realized, that I just forgot NotificationCenter.default.removeObserver on deinit inside a Super Class I use.

Well, exactly the same happens when using closures, so I suppose this doesn't relate to clearing observers.

excuse me,When will this problem be solved?

kslowbullet avatar Aug 16 '19 10:08 kslowbullet

@cxxcapf This is an open source project, so please feel free to contribute a fix if you have one.

ashleymills avatar Aug 16 '19 10:08 ashleymills