Skip to content

Commit

Permalink
Fix memory leak in NetworkReachabilityManager. (#3180)
Browse files Browse the repository at this point in the history
* Fix memory leak.

* Update deinitialization test for NetworkReachabilityManager.

rename

* Add assertion, clean up formatting.

* Make deinit tests more resilient.

Co-authored-by: Jon Shier <jon@jonshier.com>
  • Loading branch information
dirtmelon and jshier authored May 15, 2020
1 parent 505babf commit e217713
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Source/NetworkReachabilityManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ open class NetworkReachabilityManager {
}

var context = SCNetworkReachabilityContext(version: 0,
info: Unmanaged.passRetained(self).toOpaque(),
info: Unmanaged.passUnretained(self).toOpaque(),
retain: nil,
release: nil,
copyDescription: nil)
Expand Down
20 changes: 18 additions & 2 deletions Tests/NetworkReachabilityManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,40 @@ final class NetworkReachabilityManagerTestCase: BaseTestCase {

func testThatHostManagerCanBeDeinitialized() {
// Given
let expect = expectation(description: "reachability queue should clear")
var manager: NetworkReachabilityManager? = NetworkReachabilityManager(host: "localhost")
weak var weakManager = manager

// When
manager?.startListening(onUpdatePerforming: { _ in })
manager?.stopListening()
manager?.reachabilityQueue.async { expect.fulfill() }
manager = nil

waitForExpectations(timeout: timeout)

// Then
XCTAssertNil(manager)
XCTAssertNil(manager, "strong reference should be nil")
XCTAssertNil(weakManager, "weak reference should be nil")
}

func testThatAddressManagerCanBeDeinitialized() {
// Given
let expect = expectation(description: "reachability queue should clear")
var manager: NetworkReachabilityManager? = NetworkReachabilityManager()
weak var weakManager = manager

// When
manager?.startListening(onUpdatePerforming: { _ in })
manager?.stopListening()
manager?.reachabilityQueue.async { expect.fulfill() }
manager = nil

waitForExpectations(timeout: timeout)

// Then
XCTAssertNil(manager)
XCTAssertNil(manager, "strong reference should be nil")
XCTAssertNil(weakManager, "weak reference should be nil")
}

// MARK: - Listener
Expand Down

0 comments on commit e217713

Please sign in to comment.