0

J'essaie d'ajouter une barre de recherche à mon UITableView. J'ai suivi ce tutoriel: http://clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html.Problèmes UITableView et SearchBar

Je reçois cette erreur si je tape une lettre dans la zone de recherche: Rooster(10787,0xa05ed4e0) malloc: *** error for object 0x3b5f160: double free *** set a breakpoint in malloc_error_break to debug.

Cette erreur se produit ici:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    [self handleSearchForTerm:searchString]; 

    return YES; 
} 

(sur la deuxième ligne)

- (void)handleSearchForTerm:(NSString *)searchTerm { 
    [self setSavedSearchTerm:searchTerm]; 

    if ([self searchResults] == nil) { 
     NSMutableArray *array = [[NSMutableArray alloc] init]; 
     [self setSearchResults:array]; 
     [array release]; 
    } 

    //Empty the searchResults array 
    [[self searchResults] removeAllObjects]; 

    //Check if the searchTerm doesn't equal zero... 
    if ([[self savedSearchTerm] length] != 0) { 
     //Search the whole tableList (datasource) 
     for (NSString *currentString in tableList) { 
      NSString *klasString = [[NSString alloc] init]; 
      NSInteger i = [[leerlingNaarKlasList objectAtIndex:[tableList indexOfObject:currentString]] integerValue]; 
      if(i != -1) { 
       klasString = [klassenList objectAtIndex:(i - 1)]; 
      } 

      //Check if the string matched or the klas (group of school) 
      if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound || 
       [klasString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound) { 
       //Add to results 
       [[self searchResults] addObject:currentString]; 

       //Save the klas (group of school). It has the same index as the result (lastname) 
       NSString *strI = [[NSString alloc] initWithFormat:@"%i", i]; 
       [[self searchResultsLeerlingNaarKlas] addObject:strI]; 
       [strI release]; 
      } 

      [klasString release]; 
     } 
    } 
} 

que quelqu'un peut me aider?

Cordialement, Dodo

Répondre

2

La double erreur libre signifie que vous avez sorti un objet plus que nécessaire. Ici, l'objet suspect est klasString. A partir de votre code:

NSString *klasString = [[NSString alloc] init]; 
... 
if(i != -1) { 
    klasString = [klassenList objectAtIndex:(i - 1)]; 
} 
... 
[klasString release]; 

L'affectation dans l'instruction if

  1. perd référence au nouveau alloué NSString, l'introduction d'une fuite de mémoire
  2. rend la version ultérieure appliquer à l'objet de klassenList. Lorsque klassenList libère ses éléments, une double erreur libre se produit.
+0

Merci !! A l'intérieur de la déclaration if, j'ai ajouté une retenue! – dododedodonl

Questions connexes