Swinject/SwinjectStoryboard

Potential leak with container hierarchy

florentdouine opened this issue ยท 4 comments

Hi,

i encountered an issue with SwinjectStoryboard when i decide to use mulitple container with hierarchy.
Here is a simple test code to explain the problem :

let baseContainer = Container()

SwinjectStoryboard.defaultContainer = baseContainer

baseContainer.storyboardInitCompleted(TestViewController.self) { (r, c) in
    print("storyboardInitCompleted called")
}

weak var leaked: AnyObject?
autoreleasepool {
   var vc: UIViewController? = UIStoryboard(name: "Navigation", bundle: nil).instantiateViewController(withIdentifier: "TestViewController")
   leaked = vc
   vc = nil
}
print(leaked)

In this code, leaked is correctly equal to nil because no one refers to it.

But, if you change the container hierarchy like this :

let baseContainer = Container()
let childContainer = Container(parent: baseContainer)

SwinjectStoryboard.defaultContainer = childContainer

baseContainer.storyboardInitCompleted(TestViewController.self) { (r, c) in
    print("storyboardInitCompleted called")
}

weak var leaked: AnyObject?
autoreleasepool {
   var vc: UIViewController? = UIStoryboard(name: "Navigation", bundle: nil).instantiateViewController(withIdentifier: "TestViewController")
   leaked = vc
   vc = nil
}
print(leaked)

This time leaked is not nil. Someone retains it in the injection logic.

Thanks for the report ๐Ÿ‘ Have you by any chance checked if this happens for regular Swinject registrations as well?

No i didn't check for now sorry

This is indeed a bug in Swinject itself - resolved by Swinject#409

Fix released with Swinject 2.6.1