【问题标题】:My app crashes after multiple rotates多次旋转后我的应用程序崩溃
【发布时间】:2010-05-16 12:00:31
【问题描述】:

当我尝试旋转它多次时,我的应用程序崩溃了。我一开始以为它只是 iPhone 模拟器,所以我将应用程序加载到 iPod touch 上,连续旋转较少后它就崩溃了。我怀疑这是我的一种旋转方法中的内存泄漏。我认为导致崩溃的唯一地方是willRotateToInterfaceOrientation:duration:。我添加/扩展的唯一两种与旋转相关的方法是shouldAutorotateToInterfaceOrientation: 和willRotateToInterfaceOrientation:duration,我认为它不是第一种,因为它只包含两个词:return YES;。这是我的willRotateToInterfaceOrientation:duration: 方法,您可以查看它并查看可能的内存泄漏在哪里。

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration 
{
 UIFont *theFont;


 if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
 {
  theFont = [yearByYear.font fontWithSize:16.0];
  yearByYear.font = theFont;
  [theview setContentSize:CGSizeMake(460.0f, 635.0f)];
 }
 else
 {
  theFont = [yearByYear.font fontWithSize:10.0];
  yearByYear.font = theFont;
  [theview setContentSize:CGSizeMake(300.0f, 460.0f)];
 }

 [theFont release];
}

yearByYear 是 UITextView,而 theview 是 UIScrollView。

【问题讨论】:

  • 这样的崩溃通常是过度释放对象,而不是泄漏。泄漏需要很长时间才能填满内存(除非是洪水),而且您会首先收到内存警告。

标签: iphone objective-c crash autorotate memory-leaks


【解决方案1】:

你不应该发布theFont。您不拥有该对象。

您还可以简化您正在做的事情:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration  {

   if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) {
      yearByYear.font = [yearByYear.font fontWithSize:16.0]
      [theview setContentSize:CGSizeMake(460.0f, 635.0f)];
   }
   else
   {
      yearByYear.font = [yearByYear.font fontWithSize:10.0]
      [theview setContentSize:CGSizeMake(300.0f, 460.0f)];
   }
}

彻底摆脱theFont。

【讨论】:

  • 是的。为了超级清楚,根据 Cocoa 内存指南(手边没有链接,你应该用谷歌搜索),因为你没有明确地 allocing 或 newing 或 copying 字体,你不应该释放它(除非你自己先保留它)。
  • 请注意 yearByYear 应该在其 setter 方法中适当地保留和释放字体。如果方法被合成并且具有正确的属性属性,这将为您完成......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多