iphone - iOS Development: Why are the retain counts for my view controller so strange? -


i'm diving ios development , i'm building navigation based app wasn't releasing 1 of views being pushed onto nav stack. problematic because view controller never being deallocated, memory uses builds every time view controller pushed on stack. after investigating issue, found retain counts view controller strange. view controller in question pushed on stack once countdown timer reaches zero.

here's code creates view controller in timer callback, displays retain count, , pushes onto nav stack...

-(void)updatecountdowntimer   //defined in myviewcontroller_a class {     [self settimeremaining:([self timeremaining] - 1)];      [[self countdownlabel] setalpha:1];     [[self countdownlabel] settext:[nsstring stringwithformat:@"%d", [self timeremaining]]];      //fade out current time     [uiview beginanimations:@"fadeanimation" context:nil];     [uiview setanimationduration:1];     [[self countdownlabel] setalpha:0];     [uiview commitanimations];        if ([self timeremaining] == 0)      {                myviewcontroller_b *myvc_b = [[myviewcontroller_b alloc] initwithnibname:@"myview_b_iphone" bundle:nil];         [[self navigationcontroller] pushviewcontroller:myvc_b animated:yes];         nslog(@"updatecountdowntimer: %d", [myvc_b retaincount]);         [myvc_b release];          [[self countdowntimer] invalidate];         [[self countdownlabel] sethidden:yes];     } } 

and here's code pops view controller off nav stack once pause button pressed...

- (void)pausebuttonpressed:(id)sender {     //stop timer     [puzzletimer invalidate];      nslog(@"pausebuttonpressed before pop: %d", [self retaincount]);      //return previous view     [[self navigationcontroller] popviewcontrolleranimated:yes];      nslog(@"pausebuttonpressed after pop: %d", [self retaincount]); } 

and here's console output shows strange retain counts throughout process...

2010-12-02 17:50:38.062 myapp[821:307] updatecountdowntimer: 5 2010-12-02 17:50:40.453 myapp[821:307] pausebuttonpressed before pop: 2 2010-12-02 17:50:40.462 myapp[821:307] pausebuttonpressed after pop: 4 

i'm new ios development, code seems pretty straightforward me, don't know i'm missing.

thanks in advance wisdom!

update: looks leaks instrument reporting leak on line of code pushes previous view controller onto stack (that is, view controller responsible pushing view controller in question). code once again straightforward, don't know why it's reporting leak...

myviewcontroller_a *myvc_a = [[myviewcontroller_a alloc] initwithnibname:@"myview_a_iphone" bundle:nil];  [[self navigationcontroller] pushviewcontroller:myvc_a animated:yes]; //<--leak being reported here  [myvc_a release]; 

*update:*found problem, saying , same problem shown in link posted in comments below, had live objects still referencing view controller, prevented deallocating. in case, had 2 timers targeting view controller , timers weren't being invalidated before popped view off stack, meant there 2 live objects still referencing view controller. here's snippet found in apple docs uncovered problem...

perhaps more importantly, timer maintains strong reference target. means long timer remains valid (and otherwise abide memory management rules), target not deallocated.

anyhow, again helped!

there nothing wrong code far memory management concerned.

you should not rely on retain counts check if objects being released system retaining needs , releasing when appropriate. instance when add view controller stack gets retained nav controller along subviews , when it's popped out gets send release message propagates of subviews.

the general rule if alloc, retain or copy object, responsibility release it. else dealt system , flushed auto release pool.


Comments

Popular posts from this blog

400 Bad Request on Apache/PHP AddHandler wrapper -

Add email recipient to all new Trac tickets -

php - Change action and image src url's with jQuery -