Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

John Baughman

macrumors regular
Original poster
Oct 27, 2003
100
0
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...

Code:
- (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..

Code:
- (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.

John
 
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?
 
Code:
- (void)locationSearchViewDidDismiss:(LocationSearchViewController *)controller withLocation:(NSString*)location
{
	[self dismissModalViewControllerAnimated:YES];
	
	if(location != @""){
		[lastLocation replaceObjectAtIndex:0 withObject:location];
		[myTableView reloadData];
	}	
}

What, may I ask, is the purpose of:

Code:
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.