【问题标题】:iPhone UIAlertview abandon memoryiPhone UIAlertview 放弃内存
【发布时间】:2012-11-04 04:00:17
【问题描述】:

我使用 UIAlertview 输入一些文本,它工作正常。 但是,alertview 关闭后,在打开 alertview 之前内存仍然增加了 17MB 以上(从 9MB 到 26MB)。我使用 Instrument Allocation 来测量堆内存和活动监视器来测量它。它们都显示了相似的结果。当我切换到其他应用程序时,alertview 增加的内存将减少到适当的水平。正常吗?我的环境是iPhone4+iOS6。 这是一些尝试的代码。

   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"input caption" message:@"text"
                                                   delegate:self cancelButtonTitle:@"cancel"  otherButtonTitles:@"OK", nil];
   [alert show];
   [alert release];

谢谢。


亲爱的 Naveed S 和 Purr, 非常感谢。 我不确定我是否正确遵循了您的建议。但是我用Activity monitor watch the Real mem,alertview关闭后内存还是增加了20MB左右。我在 [alert show] 之后删除 [alert release],然后写了

  [alert show];
 //[alert release];

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 { [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
  [alertView autorelease];                                                      }      

此外,当我按下 home 后应用程序进入后台模式时,内存会减少。 (这与我之前的代码类似)。我怎么能正确地观看废弃的记忆。我什至不确定如何以正确的方式观看它.....

顺便说一句,我的原始代码遵循此处的帖子。 Uialertview and memory management

【问题讨论】:

  • 我认为您不应该看到由于 UIAlertView 而导致 20MB 的内存激增。肯定有别的事情发生了……
  • 我使用instrument Activity monitor查看真实的内存,在显示alertview之前显示9.20 MB,但在关闭alertview后显示26 MB。我一次又一次地继续这样做,真实的内存稳定地显示在 26 MB 附近。当我打开 safari 或按 home 时,它​​显示 11.6MB。我猜iOS为alertview或textfield做了一些内存缓存(alertview中有一个textfield)。切换到其他应用程序时,此缓存将减少。总结一下,我觉得不正常,但是不知道怎么解决。
  • 警报关闭时调用什么代码?
  • 我不知道警报关闭时需要调用什么代码,这是我的情况 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { } - (void)alertView :(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { [textfieldCaption resignFirstResponder]; if (buttonIndex != [alertView cancelButtonIndex]) { } [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES]; // 崩溃 [alertView 发布]; }

标签: ios memory-management uialertview memory-leaks


【解决方案1】:

一方面,您应该自动释放您的 alertView。 -alertView:didDismissWithButtonIndex:UIAlertView 调用,因此当您的方法返回时,alertView 对象应该仍然有效(而不是 -release'd)。

更好的是,转换为 ARC 并手动完成 -retain-release-autorelease ;)

如果您不转换为 ARC,请执行以下操作:从原始帖子的代码中删除 -release,取消注释您的 -alertView:didDismissWithButtonIndex: 方法中的 //CRASH 并将 -release 更改为 -autorelease 那里。

【讨论】:

  • 亲爱的 Naveed S 和 Purr,非常感谢。我不确定我是否正确遵循了您的建议。但是我用Activity monitor watch the Real mem,alertview关闭后内存还是增加了20MB左右。我在 [alert show] 之后删除 [alert release],我写了 [alert show]; //[警报发布]; - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES]; [alertView 自动释放];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多