views:

24

answers:

3

Hello all,

So I am trying to implement a search bar in my app and am very close but can't seem to figure out where this memory error is occurring. This is what part of my search method looks like:

filters = [[NSMutableArray alloc] init];
NSString *searchText = detailSearch.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

// Normally holds the object (ex: 70 locations)
searchArray = self.copyOfFilters ;

//This is the line that is breaking after ~2-3 letters are entered in the search
for (NSString *sTemp in searchArray)
{
    NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

    if (titleResultsRange.length > 0)
        [filters addObject:sTemp];
}
displayedFilters = filters;

copyOfFilters is a deep copy of the displayed filters that appear when the view first loads via:

self.copyOfFilters = [[NSMutableArray alloc] initWithArray:displayedFilters copyItems:YES];    

I have traced through the entry of letters and it is accurate after 2 letters, but once you try and enter a letter after a space in the search bar, it crashes. The value of copyOfFilters = {(int)[$VAR count]} objects. Does anyone know what may be causing this? Thanks!

A: 

did you release filters while still calling displayedFilters?

boreas
Nah it was how I was initializing the search array
Geoff Baum
A: 
NSMutableArray *searchArray = [[NSMutableArray alloc] init];

// Normally holds the object (ex: 70 locations)
searchArray = self.copyOfFilters ;

is a really basic memory leak. You create a NSMuatbleArray and loose any chance to release it with the next statement.

Be sure you don't release or change copyOfFilters or searchArray anywhere in your code.

VdesmedT
Solved it: Changed above code to - NSMutableArray *searchArray = [[NSMutableArray alloc] initWithArray:copyOfFilters copyItems:YES]; Looks like I still need to work on memory management. :( Thanks for the help!
Geoff Baum
Do you know of any straightforward resources that I could go to to read up on mem management? (I don't have a book yet to reference that stuff.) Thanks
Geoff Baum
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html (you will have to log on)
VdesmedT
perfect. Thanks!
Geoff Baum
A: 

Im wondering if you are having a problem when self.copyOfFilters is modified or so. Try

searchArray = [self.copyOfFilters copy] ;

Dont forget to release searchArray after the loop. This might be a shot in the dark but might be worth a shot?

AlexejK
if copy of filter is releaseor altered , this wont makes any differences.
VdesmedT