Swift 3 & iOS 10 False Memory Error

In Xcode 8, a memory error (false error) appears when used with iOS 10 and Swift 3.

The following code reports a memory leak in the tools and the Xcode 8 memory debugger:

class SomeClass: NSObject { var view: SomeView! deinit { print("SomeClass deinit") } } class SomeView: UIView { weak var reference: SomeClass? deinit { print("SomeView deinit") } } class ViewController: UIViewController { var someProperty: SomeClass? override func viewDidLoad() { super.viewDidLoad() let c = SomeClass() let v = SomeView() c.view = v v.reference = c someProperty = c } } 
+5
source share
2 answers

I tried different options to confirm that this is really a mistake, and my findings:

  • If you do not assign c in someProperty code someProperty , both instances will print the string in the corresponding deinit s. A true strong reference loop does not fade.
  • If SomeClass not inherited from NSObject , this error does not occur.
  • When using Swift 2.2, this does not happen.
  • When using iOS 9- this does not happen.
  • Once someProperty set to nil somewhere in the code, both instances of deinit ed. The Xcode 8 memory debugger confirms that there are no memory leaks. However, in Tools, this change is not reflected - rightfully, since a real memory leak will probably not be resolved.

FYI, this happens not only when it is assigned to the UIViewController property. Initially, I learned about this behavior in one object.

+3
source

It seems to have fixed iOS 10.3 (first beta) and Xcode 8.3 (first beta).

0
source

Source: https://habr.com/ru/post/1257755/


All Articles