【问题标题】:Memory not releasing after popToRootViewControllerAnimatedpopToRootViewControllerAnimated 后内存未释放
【发布时间】:2013-08-07 20:38:32
【问题描述】:

我在我的应用程序中使用了 navigationController。在我推送并弹出视图控制器 3 次后,我的应用程序由于内存不足而崩溃。这是我下面的代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        HomePageVC *homePage = [[ViewController alloc] homePage = [[ViewController alloc] initWithNibName:@"MainView-iPad" bundle:nil];
        navigationController = [[UINavigationController alloc] initWithRootViewController:homePage];
        self.window.rootViewController = navigationController;
        [self.window makeKeyAndVisible];
        return YES;
}

当用户按下按钮时,我将他发送到另一个视图控制器。

 -(IBAction)showMap:(id)sender
 {
    MapViewController *mapViewController = Nil;
    mapViewController = [[MapViewController alloc] initWithNibName:@"MapView-iPad" bundle:nil];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.navigationController pushViewController:mapViewController animated:YES];
 }

当他想回到rootView Controller时,我做

-(IBAction)goBack:(id)sender
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.navigationController popToRootViewControllerAnimated:YES];
}

现在这样做几次之后,didReceiveLowMemory 逐渐被调用并且应用程序崩溃。

为了调试更多,我循环打印了内存使用情况。

-(void) report_memory {
    struct task_basic_info info;
    mach_msg_type_number_t sizeM = sizeof(info);
    kern_return_t kerr = task_info(mach_task_self(),
                               TASK_BASIC_INFO,
                               (task_info_t)&info,
                               &sizeM);
    if( kerr == KERN_SUCCESS ) {
        NSLog(@"Memory usage: %.4lf MB", info.resident_size/1000000.0);
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
    }
}

输出是

When the app Launches    : Memory usage: 137.8263 MB
After showMap First Time : Memory usage: 206.2172 MB
Gone Back Home           : Memory usage: 223.6580 MB   ==> MapVC didn't release

After showMap Second Time: Memory usage: 227.2172 MB
Press Go Back Home       : Memory usage: 250.2172 MB   ==> MapVC didn't release

After showMap Third Time : App Crashes

我的lowMemory写成

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    NSLog(@"Got Low Memory From %@",[self class]);
}

为了增加惊喜,我收到了来自 HomePageVC 和 MapVC 的内存不足警告。如果我已经将 MapVC 大便了,我是如何从它那里收到 lowMemory 的?为什么 MapVC 消耗的内存没有被释放?我正在使用 ARC。

【问题讨论】:

  • 其他类保留您的 MapView 对象的可能性有多大?
  • 不,我认为没有其他人可以访问它。
  • 这不是“访问”它的问题,而是引用它的问题。
  • 是的,我明白了。它没有从任何地方引用。如果有人引用它,有什么方法可以打印吗?
  • 在MapViewController中实现dealloc,并在里面放一个log看是否被调用。

标签: iphone ios objective-c


【解决方案1】:

我的代码(使用 ARC)中遇到了同样的问题,我的 UIViewController 在调用 popToRootViewControllerAnimated 后没有被释放:

我的错误是由于 UIViewController 中的 NSTimer。我试图使 dealloc 中的计时器无效,但因为我的 NSTimer 使用的是 target:self(即保留指向我的 UIViewController 的指针),所以从未调用 dealloc。

为了解决这个问题,我使 viewWillDisappear 中的计时器失效。

【讨论】:

    【解决方案2】:

    所以看起来除非需要,否则 iOS 不会释放内存。我添加了更多代码来分配大量内存。当视图控制器被弹出时,内存从未被释放。但是当 didReceiveMemorywarning 被调用时,实际上我恢复了内存。我从这个练习中学到的另一件事是当它不是来自苹果教程here 的活动窗口时释放视图。

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        if ([self.view window] == nil)
            self.view = nil;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-21
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多