objective c - Custom Timer Leaking Problems -


while designing app ios needed timer paused , resumed. therefor created our own timer. however, seems leak, causing cpu usage build throughout app. below entire code timer. appreciated. also, we're using arc , we're kind of new concept, might causing retain cycle somewhere.

@implementation pausibletimer {     bool haspausedthiscycle; }  + (pausibletimer *)scheduledtimerwithtimeinterval:(nstimeinterval)timeinterval target:(id)target selector:(sel)selector userinfo:(id)userinfo repeats:(bool)repeats {     pausibletimer *newtimer = [[pausibletimer alloc] init];     newtimer.timeinterval = timeinterval;     newtimer.target = target;     newtimer.selector = selector;     newtimer.userinfo = userinfo;     newtimer.repeats = repeats;      return newtimer; }  - (void)start {     [self.timer invalidate]; // invalidate current timer      // initialize nstimer pausibletimer conditions     self.timer = [nstimer scheduledtimerwithtimeinterval:self.timeinterval target:self selector:@selector(timerfired:) userinfo:self.userinfo repeats:self.repeats];      self.ispaused = no; // set ispaused no }  - (void)pause {     // if timer paused, return     if (self.ispaused)     {         return;     }      self.ispaused = yes; // set ispaused yes     haspausedthiscycle = yes; // set haspausedthiscycle yes      [self.timer invalidate]; // invalidate current timer }  - (void)timerfired:(nstimer *)timer {     // if timer paused, return     if (self.ispaused)     {         return;     }      // ignore performselector leaks #pragma clang diagnostic push #pragma clang diagnostic ignored "-warc-performselector-leaks"     if (self.selector)     {         [self.target performselector:self.selector withobject:self];     }     else if (!self.selector)     {         [self invalidate];     } #pragma clang diagnostic pop      if (haspausedthiscycle)     {         haspausedthiscycle = no; // set haspausedthiscycle no          if (self.repeats)         {             // set new nstimer original timeinterval             [self.timer invalidate];             [self start];         }     } }  - (void)invalidate {     [self.timer invalidate];     self.timer = nil;     self.selector = nil;     self.target = nil;     self.userinfo = nil; }  @end 

update

here's header file, showing declaration of variables. had target set weak beginning. i've gathered using instruments, time profiler, points me pausibletimer , digging deeper takes me called mk_timer_arm, supposedly still running after have killed can. invalidate , set of timers nil within viewwilldisappear. anyways, here's header , appreciate help.

#import <foundation/foundation.h>  @interface pausibletimer : nsobject  // set nstimer properties @property (nonatomic) nstimeinterval timeinterval; @property (nonatomic, weak) id target; @property (nonatomic) sel selector; @property (nonatomic) id userinfo; @property (nonatomic) bool repeats;  @property (strong, nonatomic) nstimer *timer; // pointer current timer @property (nonatomic) bool ispaused; // check if timer paused  // pausibletimer initializer + (pausibletimer *)scheduledtimerwithtimeinterval:(nstimeinterval)timeinterval target:(id)target selector:(sel)selector userinfo:(id)userinfo repeats:(bool)repeats;  - (void)pause; - (void)start; - (void)invalidate;  @end 

you did not explain leaking, give 2 possible considerations arise code:

self.target

you not show property declaration instance variables, if self.target not explicitly declared weak have retain cycle , leak right there. suppose imagine view controller or similar object has pausibletimer instance variable. view controller retains pausibletimer. pausibletimer retains view controller target. presto, retain cycle, leak.

self.timer

in general, there big problem nstimer under arc. until invalidated, timer retains target. in case, target of nstimer pausibletimer instance. if pausibletimer has nstimer (self.timer) @ time owning object (the view controller or whatever) goes out of existence, if arc sends release pausibletimer in order, still, unfortunately, nstimer still has retain on pausibletimer.

solution

if second of things true, have retain cycle. if first true, have double retain cycle.

you cannot solve implementing dealloc in pausibletimer invalidate nstimer, because dealloc won't called; whole problem. similarly, cannot solve implementing dealloc in view controller invalidate pausibletimer, because (again) dealloc won't called; (again) whole problem.

basically have taken memory management problem nstimer , pushed 1 stage, in way aggravates it.

the way solve normal nstimer owned view controller explicitly invalidate timer before view controller goes out of existence, e.g. in viewwilldisappear:. causes nstimer release target, can later go out of existence in order. presumably have that.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -