3

The Parent class below has strong & weak references to a single child object. The child object is never freed. Remove the weak reference and the child is freed.

Calling the Mirror method is required to make this leak, but I cannot understand why using a Mirror would cause this behavior. The results of the Mirror are not retained.

With both weak & strong refs, only the parent deinit is executed!

Parent deinit

I expect to see both parent & child objects freed, so that the log says:

Parent deinit
DeinitLogger deinit

Remove the weak reference and the log shows both objects deinit, as expected.

Can you help me understand why this is leaking? (This is not a playground, but in an app).

class DeinitLogger {
    deinit {
        print("DeinitLogger \(#function)")
    }
}

class Parent: NSObject {

    weak var weakLogger: DeinitLogger?
    var strongLogger: DeinitLogger

    override init() {

        let logger = DeinitLogger()

        // Create a weak ref
        weakLogger = logger  // comment out this line, no leak!

        // Create a strong ref to same object.
        strongLogger = logger

        super.init()

        // Invoking mirror and adding the properties to a dict leaks when one of the
        // properties is weak.
        let dict = dictionaryOfProps()
        print(dict)
    }

    deinit {
        print("Parent \(#function)")
    }

    /// Generates a dictionary of property names -> properties
    /// e.g. "strongLogger" -> type of strongLogger.
    private func dictionaryOfProps() -> [String: Any] {
        var result = [String: Any]()
        let mirror = Mirror(reflecting: self)
        for case let(label?, value) in mirror.children {
            result[label] = value
        }
        return result
    }
}

// Chuck these two lines in a viewDidLoad(), or anywhere. 
var o: Parent? = Parent()
o = nil // everything should be freed here.
5
  • I found this issue in an App, and boiled it down into a playground. But of course, translating this code back to App and it does work properly. Argh! Back to boiling down. Thank you. Jan 8, 2019 at 21:08
  • 1
    Alright... I went back to an app and changed my boiling-down recipe. Now there's a Mirror involved. And the weak ref + mirror causes a leak even in an app! Jan 8, 2019 at 21:28
  • 2
    I can reproduce the issue, but I admit that I have no idea what is happening here. – Instead of let dict = dictionaryOfProps(); print(dict) it suffices to execute for _ in Mirror(reflecting: self).children { } in the Parent.init method.
    – Martin R
    Jan 8, 2019 at 21:56
  • Yes, one does have to include some iteration of the children. Thanks for checking my eyes. It's not obvious, is it. I think I shall open a bug against Swift. Jan 8, 2019 at 22:08
  • 1
    That is bugs.swift.org/browse/SR-8878, it is already fixed in the development branch.
    – Martin R
    Jan 8, 2019 at 22:15

1 Answer 1

4

That is a known bug in Swift 4.2 (Xcode 10):

caused by a missing release when reading a weak reference in ReflectionMirror.m. The bug has been fixed on the master branch. Your program behaves correctly with the current “Trunk Development (master)” snapshot from https://swift.org/download/#releases.

1
  • Excellent. Thank you for following through with that research, Martin. Jan 9, 2019 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.