title: how to fix uipageviewcontroller exc_bad_access if page turn is not completed?
body: <p>I'm getting an exc_bad_access in my app and I can't figure out how to fix it.
  I just upgraded to Xcode 4.5 and I'm targeting IOS 5.0. This is also my first time using UIPageViewController.  I prefer to use storyboard as much as possible. </p>
  
  <p>What I'm trying to do is recreate a golf course flip book that contains an image on which you can scroll and zoom. I've basically got a mashup now of several tutorials that is mostly working;</p>
  
  <p>1) I have setup a UIPageviewController, which loads a UIScrollview, which adds a imageView to it. 
  2) flipping by gesture or tap is working, scrolling is working, I have pinch zoom working and a custom single and two finger tapping working for zoom in / out. 
  3) The crash appears when you start to flip the page with a sliding gesture, but then release your finger. This basically cancels the flip but then a msg gets send to a zombied object.</p>
  
  <p>Here is my 'GuideViewController.h', it acts as the datasource as well as the root.</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import "YardageHoleViewController.h"
  
  @interface GuideViewController : UIViewController &lt;UIPageViewControllerDataSource&gt;
  
  @property (strong, nonatomic) UIPageViewController *pageController;
  @property (strong, nonatomic) NSArray *pageContent;
  
  - (YardageHoleViewController *)viewControllerAtIndex:(NSUInteger)index storyboard (UIStoryboard *)storyboard;
  - (NSUInteger)indexOfViewController:(YardageHoleViewController *)viewController;
  
  @end
  </code></pre>
  
  <p>And here is the Implementation</p>
  
  <pre><code>#import "GuideViewController.h"
  #import "GolfCourseAppDelegate.h"
  #import "Hole.h"
  @interface GuideViewController ()
  @end
  
  @implementation GuideViewController
  @synthesize pageContent = _pageContent;
  @synthesize pageController = _pageController;
  
  - (void)viewWillDisappear:(BOOL)animated
  {
  [[[GolfCourseAppDelegate sharedDelegate] locationManager] stopUpdatingLocation];
  }
  
  - (void)viewDidLoad
  {
  [super viewDidLoad];
  [[[GolfCourseAppDelegate sharedDelegate] locationManager] startUpdatingLocation];
  [self createContentPages];
  
  NSDictionary *options =
  [NSDictionary dictionaryWithObject:
   [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin]
                              forKey: UIPageViewControllerOptionSpineLocationKey];
  
  self.pageController = [[UIPageViewController alloc]
  initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationVertical options: options];
  
  //self.pageController.delegate = self;
  
  self.pageController.dataSource = self;
  [[self.pageController view] setFrame:[[self view] bounds]];
  
  YardageHoleViewController *initialViewController = [self viewControllerAtIndex:0 storyboard:self.storyboard];
  
  NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
  
  [self.pageController setViewControllers:viewControllers
                           direction:UIPageViewControllerNavigationDirectionForward
                            animated:NO
                          completion:NULL];
  
  [self addChildViewController:self.pageController];
  [[self view] addSubview:[self.pageController view]];
  [self.pageController didMoveToParentViewController:self];
  
  }
  
  - (YardageHoleViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard
  {
  NSLog(@"getting data view controller at index: %d", index);
  // Return the data view controller for the given index.
  if (([self.pageContent count] == 0) || (index &gt;= [self.pageContent count])) {
      return nil;
  }
  
  // Create a new view controller and pass suitable data.
  YardageHoleViewController *yardageHoleViewController = [storyboard instantiateViewControllerWithIdentifier:@"YardageHoleViewController"];
  yardageHoleViewController.dataObject = [self.pageContent objectAtIndex:index];
  return yardageHoleViewController;
  }
  
  - (NSUInteger)indexOfViewController:(YardageHoleViewController *)viewController
  {
  // Return the index of the given data view controller.
  // For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
  NSLog(@"returning indexOfViewController : %d", [self.pageContent indexOfObject:viewController.dataObject]);
  return [self.pageContent indexOfObject:viewController.dataObject];
  }
  
  #pragma mark - Page View Controller Data Source
  
  - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
  {
  NSLog(@"getting view controller before view controller");
  NSUInteger index = [self indexOfViewController:(YardageHoleViewController *)viewController];
  if ((index == 0) || (index == NSNotFound)) {
      return nil;
  }
  
  index--;
  return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
  }
  
  - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
  {
  NSLog(@"getting view controller After view controller");
  NSUInteger index = [self indexOfViewController:(YardageHoleViewController *)viewController];
  if (index == NSNotFound) {
      return nil;
  }
  
  index++;
  if (index == [self.pageContent count]) {
      return nil;
  }
  return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
  }
  
  - (void) createContentPages
  {
  NSLog(@"creating content Pages");
  int totalHoles = [[[GolfCourseAppDelegate appData] objectForKey:@"holes"] count];
  
  NSMutableArray *holeData = [[NSMutableArray alloc] init];
  for (int i = 1; i &lt; totalHoles+1; i++)
  {
      Hole *newHole = [[Hole alloc] initWithHoleNumber:i imageUrl:[NSString stringWithFormat:@"hole%@%d.jpg", (i &lt; 10) ? @"0" : @"", i]];
      NSLog(@"Hole image url:%@",newHole.imageUrl);
      //int holeNumber = i;
      //NSString *imageUrl = [NSString stringWithFormat:@"hole%@%d.jpg", (i &lt; 10) ? @"0" : @"", i];
     [holeData addObject:newHole];
  }
  
  self.pageContent = [[NSArray alloc] initWithArray:holeData];
  NSLog(@"count of holeData %d", self.pageContent.count);
  }
  
  - (void)didReceiveMemoryWarning
  {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
  }
  @end
  </code></pre>
  
  <p>Now for the view we are flipping through 'YardageHoleViewController.h'</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import &lt;MediaPlayer/MediaPlayer.h&gt;
  #import &lt;CoreLocation/CoreLocation.h&gt;
  
  @interface YardageHoleViewController : UIViewController &lt;UIScrollViewDelegate, CLLocationManagerDelegate&gt;
  
  @property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
  @property (assign, nonatomic) int hole;
  @property (assign, nonatomic) int totalHoles;
  @property (strong, nonatomic) id dataObject;
  @property (strong, nonatomic) IBOutlet UILabel *frontLabel;
  @property (strong, nonatomic) IBOutlet UILabel *middleLabel;
  @property (strong, nonatomic) IBOutlet UILabel *backLabel;
  - (IBAction)nextPage:(id)sender;
  - (IBAction)previousPage:(id)sender;
  - (IBAction)infoPage:(id)sender;
  - (IBAction)homePage:(id)sender;
  
  - (void)updateDistanceDisplay;
  - (NSString *)formatDistance:(NSNumber *)distance;
  
  @end
  </code></pre>
  
  <p>Here you can see I've got a few things going on. There is some sub views to show distance to cup based on location etc. You also see some outlets, I wanted to have buttons at the top to navigate in addition to the gestures, right now that's not working because the gestures are overriding the button taps (another question for later). </p>
  
  <p>So here is the meat and potatoes 'YardageHoleViewController.m'</p>
  
  <pre><code>#import "YardageHoleViewController.h"
  #import "GolfCourseAppDelegate.h"
  #import "Hole.h"
  
  @interface YardageHoleViewController ()
  @property (nonatomic, strong) UIImageView *imageView;
  - (void)centerScrollViewContents;
  - (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer;
  - (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer;
  @end
  
  @implementation YardageHoleViewController
  @synthesize scrollView = _scrollView;
  @synthesize hole = _hole;
  @synthesize totalHoles = _totalHoles;
  @synthesize imageView = _imageView;
  @synthesize frontLabel = _frontLabel;
  @synthesize middleLabel = _middleLabel;
  @synthesize backLabel = _backLabel;
  @synthesize dataObject = _dataObject;
  
  /* The point of this method is to get around a slight annoyance with UIScrollView, which is: if the scroll view content size is smaller than its bounds, then it sits at the top-left rather than in the center. This method  positions the image view such that it is always in the center of the scroll view’s bounds.
   */
  - (void)centerScrollViewContents {
  CGSize boundsSize = self.scrollView.bounds.size;
  CGRect contentsFrame = self.imageView.frame;
  
  if (contentsFrame.size.width &lt; boundsSize.width) {
      contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
  } else {
      contentsFrame.origin.x = 0.0f;
  }
  
  if (contentsFrame.size.height &lt; boundsSize.height) {
      contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
  } else {
      contentsFrame.origin.y = 0.0f;
  }
  
  self.imageView.frame = contentsFrame;
  }
  
  - (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
  CGPoint pointInView = [recognizer locationInView:self.imageView];
  CGFloat newZoomScale = self.scrollView.zoomScale * 1.5f;
  newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale);
  CGSize scrollViewSize = self.scrollView.bounds.size;
  CGFloat w = scrollViewSize.width / newZoomScale;
  CGFloat h = scrollViewSize.height / newZoomScale;
  CGFloat x = pointInView.x - (w / 2.0f);
  CGFloat y = pointInView.y - (h / 2.0f);
  CGRect rectToZoomTo = CGRectMake(x, y, w, h);
  [self.scrollView zoomToRect:rectToZoomTo animated:YES];
  }
  
  - (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer {
  // Zoom out slightly, capping at the minimum zoom scale specified by the scroll view
  CGFloat newZoomScale = self.scrollView.zoomScale / 1.5f;
  newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale);
  [self.scrollView setZoomScale:newZoomScale animated:YES];
  }
  
  - (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  // Return the view that you want to zoom
  return self.imageView;
  }
  
  - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  // The scroll view has zoomed, so you need to re-center the contents
  [self centerScrollViewContents];
  }
  
  - (void)viewDidLoad {
  [super viewDidLoad];
  Hole *hole = (Hole*)self.dataObject;
  self.hole = hole.holeNumber;
  UIImage *image = [UIImage imageNamed:hole.imageUrl];
  self.imageView = [[UIImageView alloc] initWithImage:image];
  self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
  [self.scrollView addSubview:self.imageView];
  self.scrollView.contentSize = image.size;
  //Here you’re setting up two gesture recognizers: one for the double-tap to zoom in, and one for the two-finger-tap to zoom out.
  UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
  doubleTapRecognizer.numberOfTapsRequired = 2;
  doubleTapRecognizer.numberOfTouchesRequired = 1;
  [self.scrollView addGestureRecognizer:doubleTapRecognizer];
  
  UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)];
  twoFingerTapRecognizer.numberOfTapsRequired = 1;
  twoFingerTapRecognizer.numberOfTouchesRequired = 2;
  [self.scrollView addGestureRecognizer:twoFingerTapRecognizer];
  [[[GolfCourseAppDelegate sharedDelegate] locationManager] setDelegate:self];
  [self updateDistanceDisplay];
  
  self.totalHoles = [[[GolfCourseAppDelegate appData] objectForKey:@"holes"] count];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
  }
  
  - (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  
  CGRect scrollViewFrame = self.scrollView.frame;
  CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
  self.scrollView.minimumZoomScale = scaleWidth;
  self.scrollView.maximumZoomScale = 1.5f;
  self.scrollView.zoomScale = scaleWidth;
  [self centerScrollViewContents];
  }
  
  - (void) didRotate:(NSNotification *)notification {
  UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  
  if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
      NSString *moviePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"hole%@%d", (self.hole &lt; 10) ? @"0" : @"", self.hole] ofType:@"mp4"];
      MPMoviePlayerViewController *viewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
      viewController.moviePlayer.controlStyle = MPMovieControlStyleNone;
      viewController.view.backgroundColor = [UIColor blackColor];
      [self presentMoviePlayerViewControllerAnimated:viewController];
  } else {
      [self dismissMoviePlayerViewControllerAnimated];
  }
  }
  
  - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
  [self updateDistanceDisplay];
  }
  
  - (void) updateDistanceDisplay {
  
  CLLocation *userLocation = [[GolfCourseAppDelegate sharedDelegate] userLocation];
  
  if (userLocation != nil) {
      NSMutableDictionary *holeLocations = [[[GolfCourseAppDelegate appData] objectForKey:@"holes"] objectForKey:[NSString stringWithFormat:@"hole%d", self.hole]];
  
      if (round([[[holeLocations objectForKey:@"front"] objectForKey:@"lat"] floatValue]) == 0) {
          self.frontLabel.text = @"---";
      } else {
          CLLocation *frontLocation = [[CLLocation alloc] initWithLatitude:[[[holeLocations objectForKey:@"front"] objectForKey:@"lat"] floatValue] longitude:[[[holeLocations objectForKey:@"front"] objectForKey:@"lng"] floatValue]];
          if (([frontLocation distanceFromLocation:userLocation]/1000)&gt;1000){
              self.frontLabel.text = @"Out of Range";
          }else{
          self.frontLabel.text = [self formatDistance:[NSNumber numberWithFloat:([frontLocation distanceFromLocation:userLocation]/1000)]];
          }
      }
  
      if (round([[[holeLocations objectForKey:@"middle"] objectForKey:@"lat"] floatValue]) == 0) {
          self.middleLabel.text = @"---";
      } else {
          CLLocation *middleLocation = [[CLLocation alloc] initWithLatitude:[[[holeLocations objectForKey:@"middle"] objectForKey:@"lat"] floatValue] longitude:[[[holeLocations objectForKey:@"middle"] objectForKey:@"lng"] floatValue]];
          self.middleLabel.text = [self formatDistance:[NSNumber numberWithFloat:([middleLocation distanceFromLocation:userLocation]/1000)]];
  
      }
  
      if (round([[[holeLocations objectForKey:@"back"] objectForKey:@"lat"] floatValue]) == 0) {
          self.backLabel.text = @"---";
      } else {
          CLLocation *backLocation = [[CLLocation alloc] initWithLatitude:[[[holeLocations objectForKey:@"back"] objectForKey:@"lat"] floatValue] longitude:[[[holeLocations objectForKey:@"back"] objectForKey:@"lng"] floatValue]];
          self.backLabel.text = [self formatDistance:[NSNumber numberWithFloat:([backLocation distanceFromLocation:userLocation]/1000)]];
  
      }
  }
  }
  
  - (NSString *) formatDistance:(NSNumber *)distance {
  
  NSNumber *displayDistance;
  NSString *unitSuffix = @"";
  
  // Convert km to yards if prefs say so.
  NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
  
  if ([[preferences stringForKey:@"measurementUnit"] isEqualToString:@"meters"]) {
  
      distance = [NSNumber numberWithFloat:([distance floatValue]*1000.0)];
      if ([distance floatValue] &lt; 1000.0) {
          displayDistance = distance;
          unitSuffix = @"";
      } else {
          displayDistance = [NSNumber numberWithFloat:([distance floatValue]/1000.0)];
          unitSuffix = @"km";
      }
  
  } else {
  
      distance = [NSNumber numberWithFloat:([distance floatValue]*1.0936133*1000.0)];
      if ([distance floatValue] &lt; 1760.0) {
          displayDistance = distance;
          unitSuffix = @"";
      } else {
          displayDistance = [NSNumber numberWithFloat:([distance floatValue]/1760.0)];
          unitSuffix = @"mi";
      }
  
  }
  
  NSNumberFormatter *decimalStyle = [[NSNumberFormatter alloc] init];
  [decimalStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
  [decimalStyle setNumberStyle:NSNumberFormatterDecimalStyle];
  [decimalStyle setRoundingMode:NSNumberFormatterRoundFloor];
  [decimalStyle setRoundingIncrement:[NSNumber numberWithFloat:1.0]];
  
  NSString *finalDistance = [decimalStyle stringFromNumber:displayDistance];
  
  return [NSString stringWithFormat:@"%@%@", finalDistance, unitSuffix];
  }
  
  - (void)didReceiveMemoryWarning
  {
  [super didReceiveMemoryWarning];
  }
  
  - (void)viewDidUnload {
  [self setImageView:nil];
  [self setScrollView:nil];
  [self setFrontLabel:nil];
  [self setBackLabel:nil];
  [self setBackLabel:nil];
  [self setFrontLabel:nil];
  [self setMiddleLabel:nil];
  [super viewDidUnload];
  }
  - (IBAction)nextPage:(id)sender {
  //TODO
  // [((UIPageViewController*)self.parentViewController) setViewControllers:
  // target direction:UIPageViewControllerNavigationForward completion:nil];
  }
  
  - (IBAction)previousPage:(id)sender {
  //TODO
  //  [((UIPageViewController*)self.parentViewController) setViewControllers:&lt;#(NSArray *)#&gt; direction:UIPageViewControllerNavigationDirectionReverse animated:true completion:nil];
  }
  
  - (IBAction)infoPage:(id)sender {
  //TODO
  }
  
  - (IBAction)homePage:(id)sender {
  [self.navigationController popViewControllerAnimated:YES];
  }
  @end
  </code></pre>
  
  <p>Whew! lots of reading.  So what I tried first was to set the exception break point. No luck. Then I added lots of NSlog statements to see where we crash, and then finally looked for zombies in instruments. Here we see on the malloc "An Objective-C message was sent to a deallocated object (zombie) at address: 0x1386e0e0" in the YardageHoleViewController.</p>
  
  <p>From my NSLog statements I can see that a successful page turn looks like this;
  2012-12-16 13:33:52.280 BAP Template[1365:13a03] getting data view controller at index: 0
  //started flip right here >
  2012-12-16 13:34:06.289 BAP Template[1365:13a03] getting view controller After view controller
  2012-12-16 13:34:06.290 BAP Template[1365:13a03] returning indexOfViewController : 0
  2012-12-16 13:34:06.292 BAP Template[1365:13a03] getting data view controller at index: 1</p>
  
  <p>and here is what happens when you start to flip then release
  2012-12-16 13:36:18.613 BAP Template[1365:13a03] getting data view controller at index: 0
  //started flip then released 
  2012-12-16 13:36:21.828 BAP Template[1365:13a03] getting view controller After view controller
  2012-12-16 13:36:21.829 BAP Template[1365:13a03] returning indexOfViewController : 0
  2012-12-16 13:36:21.831 BAP Template[1365:13a03] getting data view controller at index: 1</p>
  
  <p>So in some sense it is trying to act like it completed the flip, but we didn't, and then that's when we have a bad time =(</p>
  
  <p>I've set everything to strong, and I really don't know what to try next?</p>
  
  <p>Any suggestions on my code in general would really be appreciated. Thanks in advance!
  <img src="https://i.stack.imgur.com/C9HEz.png" alt="profile of zombie"></p>
  
  <p><strong>UPDATE</strong> I looked at the crash log in organizer
  Thread 0 name:  Dispatch queue: com.apple.main-thread
  Thread 0 Crashed:
  0   libobjc.A.dylib   0x3737bf78 objc_msgSend + 16
  1   CoreLocation      0x3405ddc0 -[CLLocationManager onClientEventLocation:] + 1136
  2   CoreLocation      0x3405d77e -[CLLocationManager onClientEvent:supportInfo:] + 194
  3   CoreLocation      0x34057e38 __CLClientInvokeCallback_block_invoke_0 + 48
  I've since commented out the two lines that start/stop updating location in viewDidLoad / willDisappear in guideViewController.</p>
  
  <p>No more crashing, but why?</p>