7

I am having a zombie while using a UISearchDisplayController with a UITableView.

The UISearchDisplayController (and the rest of the view) is set via interface builder (storyboard on xcode 5 and using ARC and iOS 7 only).

The searchDisplayController is used by these 2 methods

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    return YES;
}

Instruments is giving me this information

#   Event Type  ∆ RefCt RefCt   Timestamp   Responsible Library Responsible Caller
0   Malloc  +1  1   00:10.739.188   UIKit   -[UITableView setTableHeaderBackgroundColor:]
1   Retain  +1  2   00:10.739.214   UIKit   -[UIView(Internal) _addSubview:positioned:relativeTo:]
2   Retain  +1  3   00:10.739.234   UIKit   -[UISearchDisplayController _configureSearchBarForTableView]
3   Retain  +1  4   00:10.739.291   UIKit   -[UIView(Hierarchy) subviews]
4   Retain  +1  5   00:10.771.238   UIKit   -[UIView(Hierarchy) subviews]
5   Retain  +1  6   00:10.782.890   QuartzCore  -[CALayer layoutSublayers]
6   Release -1  5   00:10.782.891   QuartzCore  -[CALayer layoutSublayers]
7   Release -1  4   00:10.792.538   QuartzCore  CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*)
8   Release -1  3   00:15.446.447   UIKit   -[UITableView dealloc]
9   Release -1  2   00:15.446.661   UIKit   -[UIView(Internal) _invalidateSubviewCache]
10  Release -1  1   00:15.446.679   UIKit   -[UIView(Hierarchy) removeFromSuperview]
11  Release -1  0   00:15.446.744   UIKit   -[UITableView setTableHeaderBackgroundColor:]
12  Zombie      -1  00:15.446.765   UIKit   -[UISearchDisplayController _cleanUpSearchBar]

I tried to clean up the UISearchDisplayController in the dealloc method like this

-(void)dealloc {
    self.searchDisplayController.searchResultsTableView.delegate = nil;
    self.searchDisplayController.delegate = nil;
    self.searchDisplayController.searchBar.delegate = nil;
    self.searchDisplayController.searchResultsDelegate = nil;
    self.searchDisplayController.searchResultsDataSource = nil;
}

but it didn't work.

Have you any idea of what I am doing wrong ?

Thanks for the help.

Bou
  • 333
  • 2
  • 10

3 Answers3

11

I think I've been able to work around this problem. Here's another question talks about workarounds:

UISearchDisplayController causes crash after viewDidUnload

So I added:

@property (nonatomic, weak) IBOutlet UISearchBar *searchBar;
@property (nonatomic) UISearchDisplayController *searchController;

And then in viewDidLoad:

UISearchDisplayController *searchController = [[UISearchDisplayController alloc] initWithSearchBar:[self searchBar] contentsController:self];
[searchController setDelegate:self];
[searchController setSearchResultsDelegate:self];
[searchController setSearchResultsDataSource:self];
[self setSearchController:searchController];

And in dealloc:

[self setSearchController:nil];

That seems to have solved it.

Community
  • 1
  • 1
BenRB
  • 330
  • 2
  • 3
  • 11
  • Thanks, it works perfectly but I confess that I don't understand why. Could you explain me how it solves this problem ? Thanks again :) – Bou Oct 09 '13 at 21:01
  • I know what you mean. I don't really understand why it works myself. My iOS 7 workarounds like this are piling up. – BenRB Oct 10 '13 at 15:31
  • Same here... Workaround works but I want to understand the reason of this problem. – GxocT Feb 25 '14 at 11:28
1

Possibly a known issue relative to searchDisplayController not being retained. Try this to increment the retain count:

in .h:

@property (nonatomic, strong) UISearchDisplayController *searchController;

in viewDidLoad:

self.searchController = (__bridge UISearchDisplayController *)(CFBridgingRetain(self.searchDisplayController));

in dealloc (not sure if needed):

self.searchController = CFBridgingRelease((__bridge void*)(self.searchController));
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Well it works when i'm doing an ad hoc release but crashes in debug mode. Any idea on how I can fix this ? – Bou Oct 07 '13 at 19:41
1
- (void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    self.tableView = nil;
}
Mazen Kasser
  • 3,559
  • 2
  • 25
  • 35