Memory Leak Indicated when Derived from NSObject


(Chris Chirogene) #1

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()
    // ...


(Ray Fix) #2

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. :thinking:

Ray

···

On Oct 14, 2016, at 9:42 AM, Chris Chirogene via swift-users <swift-users@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Chris Chirogene) #3

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@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. :thinking:

Ray

On Oct 14, 2016, at 9:42 AM, Chris Chirogene via swift-users <swift-users@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Ray Fix) #4

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

:smile:

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@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@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. :thinking:

Ray

On Oct 14, 2016, at 9:42 AM, Chris Chirogene via swift-users <swift-users@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Chris Chirogene) #5

Wow. Thanks for the info. I am indeed seeing the leak on the iPad but not in the simulator. Same with your code. I am using the newest Xcode 8.2.1 (8C1002)
Please keep me posted and I’ll do the same.
Thanks!
Chris

···

On Dec 20, 2016, at 2:02 AM, Ray Fix <rayfix@gmail.com<mailto:rayfix@gmail.com>> wrote:

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

:smile:

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

<Screen Shot 2016-12-19 at 4.53.03 PM.png>

On Dec 17, 2016, at 12:12 AM, Chris Chirogene <cchiroge@adobe.com<mailto:cchiroge@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@gmail.com<mailto:rayfix@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. :thinking:

Ray

On Oct 14, 2016, at 9:42 AM, Chris Chirogene via swift-users <swift-users@swift.org<mailto:swift-users@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@swift.org<mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users