Swinject: Using weak scope is leaking objects?

Created on 23 May 2017  路  11Comments  路  Source: Swinject/Swinject

I have this dummy object

protocol DevTestProtocol: class {}
class DevTest: DevTestProtocol {}

which is registered like follows (I'm using an assembly)

container.register(DevTestProtocol.self) { _ in
    return DevTest()
}
.inObjectScope(.weak)

For testing purposes I added a property in my app delegate

var devTest: DevTestProtocol?

and in applicationDidFinishLaunchingWithOptions I resolve the object then forget about it

self.devTest = DI.resolve(DevTestProtocol.self)!
self.devTest = nil

Is the DevTest instance supposed to be gone by now?
Because it's not. It still shows in the debug memory graph as a leak (see screenshot).
screen shot 2017-05-23 at 11 31 43

I guess this isn't expected behaviour? Am I missing something?
It looks like a bug to me and it's messing up my whole setup.
What can we do about this?

bug

Most helpful comment

Ok, I can look into adding some tests for this (if there aren't already some), and see if I can track down the problem.

All 11 comments

Can you post the DevTest class as well? Are there any retain cycles that are keeping it alive?

That's what I'm wondering. At least they don't show up in the debug memory graph.
This actually happens with the empty implementation of DevTest I provided at the top of my post.

Ok, I can look into adding some tests for this (if there aren't already some), and see if I can track down the problem.

Allright thanks, you can checkout this project to reproduce the issue if needed: https://github.com/alexisbronchart/SwinjectWeakLeak

I just found that if DevTest inherits from NSObject then the leak doesn't show up in the debug memory graph.

Thanks @alexisbronchart and @mpdifran!

UPDATED: There is a bug in Memory Profiling instruments in Xcode v8.x. DevTest object life time is correct. Instruments doesn't update the memory state. (@alexisbronchart, I changed your example a little bit) See screenshot attached
screen shot 2017-07-31 at 4 01 27 pm

Thanks @igorkotkovets for the investigation 馃憤馃憤馃憤 I'll check it.

Any updates on this issue? I'm seeing it as well. I'm not sure it's an Xcode8 profiling issue. I started noticing it in the app, then confirmed it with the profiler.

Ok, I tried the leak sample project and it doesn't appear to be leaking for me.

My leak happens when I use SwinjectStoryboard. I'll keep digging to make sue it's not on my end. And if not, I'll raise a bug for SwinjectStoryboard.

Thanks @jodyscho for the investigation馃槂 I鈥檓 going to close this issue in Swinject project.

Please anyone feel free to reopen this issue, or open an issue in SwinjectStoryboard project if you find the leak issue is reproducible馃憤

Was this page helpful?
0 / 5 - 0 ratings