Objective C Speeding up NSCFString with NSMutableArrays

I am clearing my code in an iPhone phonebook app and the Leaks tool in Tools reports that I am missing NSCFString objects. Here's the pattern I'm following:

I have a Person class in my application that has nothing more than local NSString members and associated properties for first name, last name, etc.

My view controller has an NSMutableArray property that is populated from the SQLite database in the searchBarSearchButtonClicked event. This NSMutableArray is filled with Person objects that will be used to populate my tableview control. Additionally, if the user clicks on a person in the view, their Person object will be passed to the Detail view to view additional information, not just the name.

When I do my first search and display the results, there are no memory leaks.

Now when I do my second search, I would ideally like to clear the NSMutableArray and reload it with a new result set without leaking memory. So for this I call removeAllObjects on my own personList attribute and then call the database to repopulate the personList NSMutableArray like below:

[self.personList removeAllObjects];
self.personList = [SearchService GetPersonList:searchText];
[list reloadData];

      

By calling removeAllObject, I got rid of the leaks I was using related to Person objects. However, it looks like I am now leaking NSString objects left over from properties of individual Person objects.

Is it possible?

I'm new to the Tools tool, but from what I can tell from the expanded detail when I drill into one of the NCSFString leaks, the last line of code on the stack often points to the @synthesize line of code for a property, like this:

@synthesize firstName;

      

So this is why I think these NSStrings are not getting cleaned up. Is there a better way to do this without creating memory leaks?

+2


source to share


1 answer


Will you release NSString

in a method dealloc

for your Person class?

Assuming you set up your property like this:

@property (retain) NSString *firstName;

      

When you install firstName

using the installer, it will be saved. If the instance is Person

then freed and released but has firstName

not been released, it will leak.



Put it in a method dealloc

in your class Person

:

- (void)dealloc
{
    [firstName release];
    [super dealloc];
}

      

(Assuming the corresponding ivar that is used for your property firstName

is called firstName

).

+3


source







All Articles