gpt4 book ai didi

iphone - popToRootViewControllerAnimated 后内存未释放

转载 作者:行者123 更新时间:2023-11-28 18:36:25 28 4
gpt4 key购买 nike

我在我的应用程序中使用 navigationController。在我推送并弹出 View Controller 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;
}

当用户按下按钮时,我将他发送到另一个 View Controller 。

 -(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 收到了低内存警告。如果我已经 pooped 了 MapVC,我是如何从它那里接收到 lowMemory 的?为什么 MapVC 消耗的内存没有被释放?我正在使用 ARC。

最佳答案

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

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

为解决此问题,我使 viewWillDisappear 中的计时器无效。

关于iphone - popToRootViewControllerAnimated 后内存未释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18113355/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com