Potential leak with container hierarchy
florentdouine opened this issue ยท 4 comments
florentdouine commented
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.
jakubvano commented
Thanks for the report ๐ Have you by any chance checked if this happens for regular Swinject registrations as well?
florentdouine commented
No i didn't check for now sorry
jakubvano commented
This is indeed a bug in Swinject itself - resolved by Swinject#409
jakubvano commented
Fix released with Swinject 2.6.1