开发者

Releasing detail view controller causes memory problems

开发者 https://www.devze.com 2023-02-06 16:48 出处:网络
I have a split view controller based app. In the detail view controller, call it FirstViewController, when the user presses a button, I update the view controllers with a new view controller, call it

I have a split view controller based app. In the detail view controller, call it FirstViewController, when the user presses a button, I update the view controllers with a new view controller, call it SecondViewContorller, something like below:

- (void) buttonPressed:(id)sender {
     UIViewController <SubstitutableDetailViewController> *detailViewController = nil;

     SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
     ...
     detailViewController = secondVC;

     MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
     UINavigationController *nav = (UINavigationController *)[delegate.splitViewController.viewControllers objectAtIndex: 0];
     NSArray *viewControllers = [NSArray arrayWithObjects:nav, detailViewController, nil];
     self.splitViewController.viewControllers = viewControllers;

 ...

    [detailViewController release];

}

Inside the SecondViewController, at some point we have:

 MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
 UINavigationController *nav = (UINavigationController *)[delegate.splitViewController.viewControllers objectAtIndex: 0];
 NSArray *array = nav.viewControllers;
 // Retrieve the master view controller
 MasterViewController *masterVC = [array objectAtIndex:[array count] - 1];
 [masterVC selectRowManually:[NSIndexPath indexPathForRow:0 inSection:0]];

and inside the selectRowManually I initialize the FirstViewController again:

 UIViewController <SubstitutableDetailViewController> *detailViewController = nil;

 if (rowNo == 0) {
      FirstViewController *newDetailViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:ni开发者_如何学JAVAl];
      detailViewController = newDetailViewController;
 }
 ...

 UINavigationController *nav = (UINavigationController *)[delegate.splitViewController.viewControllers objectAtIndex: 0];

 // Update the split view controller's view controllers array.
 NSArray *viewControllers = [[NSArray alloc] initWithObjects:nav, detailViewController, nil];
 delegate.splitViewController.viewControllers = viewControllers;
 [viewControllers release];

 ...

 [detailViewController release];

If I simulate a memory warning at this point in time (after the FirstViewController has been displayed again), I get a

-[UIView _invalidateSubviewCache]: message sent to deallocated instance ...

with a stack trace of

#0     0x012dd057 in ___forwarding___
#1     0x012dcf22 in __forwarding_prep_0___
#2     0x00b49a55 in -[UIView dealloc]
#3     0x00bbe52a in -[UIViewController setView:]
#4     0x00bc0eec in -[UIViewController unloadViewForced:]
#5     0x00bbcb0a in -[UIViewController unloadViewIfReloadable]
#6     0x00bbc15b in -[UIViewController didReceiveMemoryWarning]
#7     0x0006aec7 in -[SecondViewController didReceiveMemoryWarning] at SecondViewController.m:385
...

where line 385 is

[super didReceiveMemoryWarning];

If inside buttonPressed method of the SecondViewController I comment the line where I release the detailViewContorller, everything works out fine, but I leak memory. If I leave that line the way it is, then in case of a memory warning, the app crashes.

What can I do?

Thanks, Mihai


I'm curious about your first block of codes where you release your detailViewController instead of secondVC? Because your secondVC was not released at the end of the method. You should release detailViewController in the dealloc method instead in the SplitViewAppDelegate since it is part of the root views of split view.

Cheers, hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号