[swift-users] Memory Leak Indicated when Derived from NSObject

Ray Fix rayfix at gmail.com
Mon Dec 19 19:02:11 CST 2016


Thanks for the update Chris.  Hmm...

So, I get memory runtime issues if I run this on an actual device iPad Air 2 (iOS 10.2) with Version 8.2 (8C38).  Can’t get it to happen on the simulator. Can’t get it to happen if I make a macOS command line tool and inspect it with the leaks command.

(I reported this as radar 29715025 but if anyone has any insights please share!  )

Thank you,
Ray Fix


😄


import UIKit


class Thing {}

class Test: NSObject
{
    static let shared = Test()
    var dictionary: [String: Thing] = [:]

    func method() {
        dictionary = ["value": Thing()]
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        Test.shared.method()
        print("Leaky leaky... click on the memory visualizer to see issues.")
    }
}


When I click the memory visualizer it shows:

Memory Issues – (3 leaked types) Group
runtime: Memory Issues – (3 leaked types): 1 instance of _NativeDictionaryStorageImpl<String, Thing> leaked
x-xcode-debug-memory-graph://7fa607cb92c0/4296: runtime: Memory Issues: 0x1700f9f80
runtime: Memory Issues – (3 leaked types): 1 instance of _NativeDictionaryStorageOwner<String, Thing> leaked
x-xcode-debug-memory-graph://7fa607cb92c0/5924: runtime: Memory Issues: 0x170271dc0
runtime: Memory Issues – (3 leaked types): 1 instance of Thing leaked
x-xcode-debug-memory-graph://7fa607cb92c0/1891: runtime: Memory Issues: 0x170019ca0




> On Dec 17, 2016, at 12:12 AM, Chris Chirogene <cchiroge at adobe.com> wrote:
> 
> Interesting. Thanks. I’ll have to try that.
> The latest Xcode 8.2 release version seems to have fixed this. I am no longer seeing the leak.
> Take care,
> Chris
> 
>> On 17 Dec 2016, at 02:33, Ray Fix <rayfix at gmail.com> wrote:
>> 
>> FWIW, seeing this too.  Also, when I boiled the project down to a macOS command line and run the “leaks" cli I don’t see the leak. 🤔  
>> 
>> Ray
>> 
>>> On Oct 14, 2016, at 9:42 AM, Chris Chirogene via swift-users <swift-users at swift.org> wrote:
>>> 
>>> Xcode8 is showing a memory leak in instruments and the memory graph. I have narrowed it down to this: deriving from NSObject produces a leak indication. I have no idea why.
>>> I need an NSObject to later use the @objc directive.
>>> The Test instance stored in the mDict Dictionary is indicated as a leak in Xcode.
>>> This is running as an iOS Single-View-Application project in the iPhone5s Simulator running iOS10.0
>>> Here is the sample code:
>>> 
>>>  import Foundation
>>> 
>>>  class Test: NSObject  // <-- derived from NSObject produces leak indication below
>>>  {
>>>      static var cTest: Test! = nil
>>>      var mDict: [String : Test] = Dictionary<String, Test>()
>>> 
>>>      static func test() -> Void {
>>>          cTest = Test()
>>>          cTest.mDict["test"] = Test() // <-- alleged leak
>>>      }
>>>  }
>>> 
>>>  class Test  // <-- NOT derived from NSObject, NO leak indication
>>>  {
>>>      static var cTest: Test! = nil
>>>      var mDict: [String : Test] = Dictionary<String, Test>()
>>> 
>>>      static func test() -> Void {
>>>          cTest = Test()
>>>          cTest.mDict["test"] = Test() // <-- NO leak
>>>      }
>>>  }
>>> 
>>>  // from AppDelegate didFinishLaunchingWithOptions
>>>  // ...
>>>      Test.test()
>>>  // ...
>>> 
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-users
>> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161219/2dfcb862/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2016-12-19 at 4.53.03 PM.png
Type: image/png
Size: 55585 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161219/2dfcb862/attachment.png>


More information about the swift-users mailing list