From 052aefd150ef750a2b8e757356cb11025ec2076e Mon Sep 17 00:00:00 2001 From: zeenbiny <4031875+zeenbiny@users.noreply.github.com> Date: Thu, 4 Nov 2021 15:13:35 +0800 Subject: [PATCH] Update BTRControl.m Memory leak optimization --- Butter/BTRControl.m | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Butter/BTRControl.m b/Butter/BTRControl.m index 8cfeb9c..e299518 100644 --- a/Butter/BTRControl.m +++ b/Butter/BTRControl.m @@ -421,6 +421,48 @@ - (void)sendActionsForControlEvents:(BTRControlEvents)events { } } +- (void)removeFromSuperview { + // free Block and trackArea + [super removeFromSuperview]; + if (self.trackingArea) { + self.needsTrackingArea= NO; + [self removeTrackingArea:self.trackingArea]; + self.trackingArea = nil; + } + + for (BTRControlAction *action in self.actions) { + if (action.block) { + action.block = nil; + } + } + if (self.actions.count) { + [self.actions removeAllObjects]; + } + if (self.content.count) { + [self.content removeAllObjects]; + } +} + +- (void)dealloc { + // free Block and trackArea + if (self.trackingArea) { + self.needsTrackingArea= NO; + [self removeTrackingArea:self.trackingArea]; + self.trackingArea = nil; + } + for (BTRControlAction *action in self.actions) { + if (action.block) { + action.block = nil; + } + } + if (self.actions.count) { + [self.actions removeAllObjects]; + } + if (self.content.count) { + [self.content removeAllObjects]; + } +} + @end @implementation BTRControlContent {