iphone - should I release NSTimer if it has a repeats attribute equal to YES? -


sometimes confused when not releasing object. have:

nstimer *timer2;  timer2 = [nstimer scheduledtimerwithtimeinterval: (5)                                          target: self                                        selector: @selector(somemethod:)                                        userinfo: nil                                         repeats: yes]; 

and method get's executed every 5 seconds is:

-(void) somemethod:(nstimer*)thetimer {    nslog(@"code got executed"); } 

i have method places nib file on root view controller:

viewcontrollerobjetivos *control = [viewcontrollerobjetivos alloc];  [control initwithnibname:@"viewcontrollerobjetivos" bundle:nil];  uinavigationcontroller *navcontrol = [[uinavigationcontroller alloc]                                       initwithrootviewcontroller:control];  [self presentmodalviewcontroller:navcontrol animated:no];  [navcontrol setnavigationbarhidden:yes];  [control release]; [navcontrol release]; 

when call last method new nib file get's placed on root view controller. , somemethod still gets called!

so confused if should release timer2 because did not use word init or alloc nor copy initialize it. suppose do? should stop call last method showed? or maybe should release whole nib file since going working new one?

the correct way stop timer calling invalidate method.

invalidate

stops receiver ever firing again , requests removal run loop.

in case, store timer2 in ivar of class , send invalidate @ proper moment, i.e., when display second nib (if not wrong). not doing leave timer running forever, witnessed.

if set repeats no, timer automatically invalidating after firing first time:

repeats

if yes, timer repeatedly reschedule until invalidated. if no, timer invalidated after fires.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -