MSCircularSlider icon indicating copy to clipboard operation
MSCircularSlider copied to clipboard

Memory Leak

Open mixerzeyu opened this issue 5 years ago • 4 comments

Hi - The circular slider is amazing!

I've noticed recently it does not cleanup properly and creates memory leaks. There seems to be a circular (no pun intended) reference somewhere within the controller, therefore it is not deallocated when the containing UIViewController exits.

You can see the memory leaks if you show memory graphs when running an Xcode project in debug mode, there would be multiple instances of the circular slider created after you open and close the UIViewController multiple times.

Let me know if you need more info.

Thanks!

mixerzeyu avatar Aug 15 '20 15:08 mixerzeyu

Hi!

Thank you for shedding some light on this issue! I just profiled the example project on this repo and you're right, there seems to be a minor leak when the UIViewController is supposedly deallocated. I haven't analyzed it in depth yet, but it does seem to be a circular reference as you speculated! I'll make sure to fix it in the next release once I get a chance.

Thanks again!

ThunderStruct avatar Aug 16 '20 23:08 ThunderStruct

Hi!

The problem is in MSCircularSlider:

func initHandle() { handle.delegate = self handle.center = {
return self.pointOnCircleAt(angle: self.angle) } }

self in closure creates retain cycle. We should use capture list here:

func initHandle() { handle.delegate = self handle.center = { [weak self] in guard let self = self else { return CGPoint(x: 0, y: 0) } return self.pointOnCircleAt(angle: self.angle) } }

Vasyltm avatar Aug 28 '20 16:08 Vasyltm

Hey @Vasyltm

You're absolutely right! The self reference in the closure does in fact create a retain cycle. Thanks for the proposed solution as well. Will include it in the next update (and credit you in the changelog)!

ThunderStruct avatar Sep 05 '20 15:09 ThunderStruct

@Vasyltm Thanks so much for posting the solution - it works like a charm! @ThunderStruct Would be great to know once this fix goes is updated :)

mixerzeyu avatar Sep 05 '20 15:09 mixerzeyu