Iphone 我的应用程序在多次旋转后崩溃

Iphone 我的应用程序在多次旋转后崩溃,iphone,objective-c,crash,autorotate,memory-leaks,Iphone,Objective C,Crash,Autorotate,Memory Leaks,当我尝试旋转应用程序超过几次时,应用程序正在崩溃。我起初以为它只是iPhone模拟器,所以我把这个应用程序加载到了iPodtouch上,在连续旋转次数减少后,它崩溃了。我怀疑这是我的一个旋转方法中的内存泄漏。我认为导致崩溃的唯一原因是willRotateToInterfaceOrientation:duration:。我添加/扩展的与旋转相关的仅有两种方法是shouldAutorotateToInterfaceOrientation:和willRotateToInterfaceOrientati

当我尝试旋转应用程序超过几次时,应用程序正在崩溃。我起初以为它只是iPhone模拟器,所以我把这个应用程序加载到了iPodtouch上,在连续旋转次数减少后,它崩溃了。我怀疑这是我的一个旋转方法中的内存泄漏。我认为导致崩溃的唯一原因是
willRotateToInterfaceOrientation:duration:
。我添加/扩展的与旋转相关的仅有两种方法是
shouldAutorotateToInterfaceOrientation:
willRotateToInterfaceOrientation:duration
,我认为这不是第一种,因为它只包含两个词:
返回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
,而该视图是一个
UIScrollView

,您不应该发布
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)];
   }
}

彻底摆脱字体。

你不应该释放字体。您不拥有该对象

您还可以简化所做的工作:

-(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)];
   }
}

彻底摆脱这一困境。

是的。为了更清楚,根据Cocoa内存指南(手头没有链接,你应该用谷歌搜索),因为你没有明确地
alloc
ing或
new
ing或
copy
ing字体,你不应该释放它(除非你自己先保留它)。请注意,yearByYear应在其setter方法中适当地保留和释放字体。如果方法是合成的,并且具有正确的属性,那么这将为您完成…是的。为了更清楚,根据Cocoa内存指南(手头没有链接,你应该用谷歌搜索),因为你没有明确地
alloc
ing或
new
ing或
copy
ing字体,你不应该释放它(除非你自己先保留它)。请注意,yearByYear应在其setter方法中适当地保留和释放字体。如果该方法是合成的,并且具有正确的属性,那么这将为您完成…像这样的崩溃通常是过度释放对象,而不是泄漏。泄漏会花费很长时间来填满内存(除非是洪水),你会首先得到内存警告。像这样的崩溃通常是过度释放对象,而不是泄漏。泄漏需要很长时间来填充内存(除非是洪水),您会首先收到内存警告。