0

I'm making an app that has a view with UITableView with UISearchDisplayController. Data comes from NSFetchedResultsController. All is working fine: I get the data, table view is being populated, search works great. The only problem is that if I search and then click on "Cancel"(without deleting the text from the UISearchBar) and then go back to previous view controller and then go to that same view with UITableView and UISearchDisplayController it crashes with this being written to the log:

2012-01-06 16:46:37.559 MyApp[9586:207] *** -[SomeRandomViewController controllerWillChangeContent:]: message sent to deallocated instance 0x778d060

I've googled and stackoverflowed for that error, tried to release NSFetchedResultsController variable, did set it and its' delegate to nil, but nothing helps.

And if I do some search, then delete the text from the search bar and then tap on "Back" and go back to that view it works just fine.

A bit of the code:

- (void)viewDidUnload {
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

  self.fetchedResultsController = nil;
  self.searchFetchedRC = nil;
  [super viewDidUnload];
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {

  self.fetchedResultsController = nil;
  [self fetchedResultsController];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
  self.searchDisplayController.searchBar.text = @"";
  self.searchDisplayController.active = NO;
  [self searchDisplayControllerWillEndSearch:self.searchDisplayController];
  return YES;
}

Any help will be appreciated

Update

Without zombies the output to the log is:

2012-01-09 09:25:48.128 Butane[17325:207] -[UITextMagnifierTimeWeightedPoint controllerWillChangeContent:]: unrecognized selector sent to instance 0x8251dc0

Update 2

Dealloc method:

- (void)dealloc {
  [self.mySearchDisplayController release];
  self.mySearchDisplayController.delegate = nil;
  self.mySearchDisplayController = nil;
  [self.fetchedResultsController release];
  self.fetchedResultsController.delegate = nil;
  self.fetchedResultsController = nil;
  [self.searchFetchedRC release];
  self.searchFetchedRC.delegate = nil;
  self.searchFetchedRC = nil;
  [self.tableView release];
  [textView release];
  self.tableView = nil;
  self.tableView.delegate = nil;
  [super dealloc];
}

mySearchDisplayController = UISearchDisplayController

fetchedResultsController and searchFetchedRC = NSFetchedResultsControllers

tableView = UITableView

textView = [HPGrowingTextView][1]

Update 3

Inspired by the answer of Tia I've set FRCs and their delegates to nil in searchDisplayControllerWillEndSearch method. Works so far so good

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {

  self.fetchedResultsController.delegate = nil;
  self.fetchedResultsController = nil;
  self.searchFetchedRC.delegate = nil;
  self.searchFetchedRC = nil;
  [self fetchedResultsController];
}
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
Novarg
  • 7,390
  • 3
  • 38
  • 74
  • [Enable zombies](http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-in-xcode) and get back to us. – Joe Jan 06 '12 at 16:23
  • http://stackoverflow.com/questions/3532861/core-data-app-crashing-with-controllerwillchangecontent-unrecognized-selector won't help? – tia Jan 06 '12 at 17:16
  • @tia thanks for the link, but it doesn't help – Novarg Jan 09 '12 at 08:19
  • @Joe updated question for what happens without zombies(they were enabled) – Novarg Jan 09 '12 at 08:35
  • I don't see your `dealloc`. Do you release the controller there? – tia Jan 09 '12 at 08:45
  • @tia question updated. I think I do release it. It does look kinda sloppy, but I'm quite new to iOS development – Novarg Jan 09 '12 at 08:59

2 Answers2

1
- (void)dealloc {
  //[self.mySearchDisplayController release];
  self.mySearchDisplayController.delegate = nil;
  self.mySearchDisplayController = nil;
  //[self.fetchedResultsController release];
  self.fetchedResultsController.delegate = nil;
  self.fetchedResultsController = nil;
  //[self.searchFetchedRC release];
  self.searchFetchedRC.delegate = nil;
  self.searchFetchedRC = nil;
  //[self.tableView release];
  [textView release];
  self.tableView.delegate = nil;
  self.tableView = nil;
  [super dealloc];
}

You are over-releasing your properties, as setting it to nil already release it for retained properties. I tried to remove extra releasing code by commenting them out, so please try to replace your dealloc with my code above and see.

tia
  • 9,518
  • 1
  • 30
  • 44
  • Thanks for your answer, but it's still the same: 2012-01-09 10:46:01.154 MyApp[19078:207] *** -[SomeRandomViewController controllerWillChangeContent:]: message sent to deallocated instance 0x7790820 – Novarg Jan 09 '12 at 09:47
  • Inspired by your answer I did set NSFetchedResultsControllers and their delegates to nil in searchDisplayControllerWillEndSearch method. Up to now it's working. Thanks for pushing me in the right direction :) – Novarg Jan 09 '12 at 10:15
  • No worries, but you should really study the document about memory management as it is quite fundamentally crucial for iOS development. – tia Jan 09 '12 at 10:37
1

I had a similar problem as my VC implemented the UISearchBarDelegate protocol and searchBarTextDidEndEditing: was being called after I'd dismissed the VC in the tableView:didSelectRowAtIndexPath: method.

I fixed it with:

self.searchDisplayController.delegate=nil;
self.searchDisplayController.searchBar.delegate=nil;

before the

[self dismissModalViewControllerAnimated:YES];
Peter
  • 1,022
  • 9
  • 12