views:

175

answers:

1

My app is crashing when I try and rotate it more than a couple of times. I first thought it was just the iPhone Simulator, so I loaded the app onto an iPod touch, and it crashed after fewer rotates in a row. I suspect it's a memory leak in one of my rotate methods. The only place I can think that the crash is being caused is in willRotateToInterfaceOrientation:duration:. The only two methods related to rotate that I've added/extended are shouldAutorotateToInterfaceOrientation: and willRotateToInterfaceOrientation:duration and I don't think it's the first because it only contains the two words: return YES;. Here is my willRotateToInterfaceOrientation:duration: method so you can review it and see where the possible memory leak is.

-(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 is a UITextView theview is a UIScrollView

if anyone has any idea of why my app is crashing, I would be greatly appreciative.

+3  A: 

You shouldn't be releasing theFont. You don't own the object.

You can also simplify what you're doing to:

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

Getting rid of theFont completely.

Tom Irving
Yes. To just to be super clear, according to the Cocoa memory guidelines (don't have link handy, you should Google for it), since you're not explicitly `alloc`ing or `new`ing or `copy`ing the font, you shouldn't release it (unless you retain it yourself first).
quixoto
Note that yearByYear *should* be retaining and releasing the font appropriately in its setter method. This will be done for you if the method is synthesized and has the right property attributes...
walkytalky
thanks so much guys, you've been a great help
Ian Rae