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

Chris Chirogene cchiroge at adobe.com
Fri Oct 14 11:42:37 CDT 2016


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



More information about the swift-users mailing list