Release of UIViewController causing crash

Discussion in 'iOS Programming' started by John Baughman, Dec 16, 2009.

  1. John Baughman macrumors regular

    Oct 27, 2003
    I have a UIViewController that I am presenting modally. I use this in several places and all except 1 work perfectly. In this one case the app crashes with a Bad Access when the modal view is dismissed. The culprit appears to be the release of the UIViewController. It is not crashing on any line of my code so has been very difficult to trace.

    The ViewController implements a delegate protocol with a call back for dismissal, and I am presenting the view like this...

    - (void)tableView:(UITableView *)tableView 
    	LocationSearchViewController *locationSearchViewController = [[LocationSearchViewController alloc] initWithNibName:@"LocationSearchViewController"	bundle:nil];
    	locationSearchViewController.delegate = self;		
    	UITableViewCell *cell = [myTableView cellForRowAtIndexPath:indexPath];
    	locationSearchViewController.defaultLocation = cell.textLabel.text;
    	[self presentModalViewController:locationSearchViewController animated:YES];
    	[locationSearchViewController release];
    The call back looks like this..

    - (void)locationSearchViewDidDismiss:(LocationSearchViewController *)controller withLocation:(NSString*)location
    	[self dismissModalViewControllerAnimated:YES];
    	if(location != @""){
    		[lastLocation replaceObjectAtIndex:0 withObject:location];
    		[myTableView reloadData];
    As is, this will crash, but if I remove [locationSearchViewController release] from the picture it works fine and "Build and Analyze" reports no issues. It also crashes if I use autorelease or move the release to the call back.

    Again it works fine with the release in other places that I am using this view. There is a difference in the way this particular implementation of the view is presented from the others. In the other cases I am presenting the view from the root view controller or from another modal view. In this case there is a modal view with a navigation bar that pushes another view on to the stack and from there the problem view is presented.

    Bottom line my question is, should I be worried about this anomaly? Or, just don't release the view in this one case and forget about it?

    Thanks for any suggestions.

  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    You should worry about this anomaly.

    Is the crash in objC_msgSend? If so it's likely that something is messaging your view controller after it's been dealloced.

    Show the stack trace of the crash.

    What happens when you turn on Zombies?

    Have you run Clang on this code?
  3. North Bronson macrumors 6502

    Oct 31, 2007
    San José
    What, may I ask, is the purpose of:

    if(location != @"")
    Are you checking to see if "location" is non-nil? Positive length?

    If you comment out the whole "if" statement and keep on releasing the view controller as you were, do you still crash?

Share This Page