Quote:
Originally Posted by BostonMerlin
there is no technical limitation. we would have to see how your instantiating the array and the calls your making.
John
|
Let me see if I can break this down. The overall thrust is that I have a large sql database with locations in the world. I pull up an array of countries in one table and the user selects one, then I go back to the next sql database and pull up a list of cities in that country (different view controller). I'm basically using the same code in both view controllers (cities and countries), they call the app delegate that has two functions that creates sql calls and return the data. I then process that list into a table and organize it into an NSmutableArray based on the first letter. This basically uses code adapted from the Iphone developers cookbook (to automatically allocate cities to the sections based on their first letter).
The same mechanism on the retrieve countries works fine with no leaks, the second produces lots of leaks depending on the number of records returned from the database. The only difference was that the second version sent an array of data to the app delegate function (which table to use, which country was selected) and I wondered if it was somehing to do with the array. However I've just adapted it so that it sends one NSString instead and it still throws a wobbly. The strange thing is:
1) the number of leaks is not consistent in instruments. it can vary by a number each time I run it the same way
2) the number of leaks seems to not be related directly to the number of records returned, it is proportionate, but not directly.
3) I only see the leaks when I leave the list, either by choosing a city in the table or by hitting back on the nav bar.
Here is viewdidload:
- (void)viewDidLoad
{
HeliosAppDelegate *delegate = (HeliosAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *array = [NSArray alloc];
array= [delegate grabCitiesWorld:country];
listData = array;
[self createSectionList];
[array release];
}
Here is the code for putting the array into the sectioned list
// Build a section/row list from the alphabetically ordered word list
- (void) createSectionList
{
sectionArray = [[NSMutableArray alloc] init];
// Build an array with 26 sub-array sections
for (int i = 0; i < 26; i++) [sectionArray addObject:[[NSMutableArray alloc] init]];
// Add each word to its alphabetical section
for (NSString *cities in listData)
{
if ([word length] == 0) continue;
// determine which letter starts the name
NSString *firstletter = [[word substringToIndex:1] uppercaseString];
NSRange range = [ALPHA rangeOfString:firstletter];
// Add the name to the proper array
[[sectionArray objectAtIndex:range.location] addObject:city];
}
}
#define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
and to prove I'm releasing the array
- (void)dealloc {
[super dealloc];
[sectionArray release];
}
Now in the app delegate here is the retrieve the list of cities code.
***app delegate
- (NSMutableArray *)grabCitiesWorld

NSString *)country {
NSMutableArray *cities = [[NSMutableArray alloc] init];
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CityData.sqlite"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
NSString *longsql = [NSString stringWithFormat:@"SELECT city FROM %@ where country = '%@' ORDER BY City", table, country];
const char *sql = [longsql UTF8String];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *cityname = [NSString stringWithUTF8String

char *) sqlite3_column_text(statement, 0)];
[cities addObject:cityname];
}
}
// "Finalize" the statement - releases the resources associated with the statement.
sqlite3_finalize(statement);
} else {
// Even though the open failed, call close to properly clean up resources.
sqlite3_close(database);
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
// Additional error handling, as appropriate...
}
sqlite3_close(database);
return cities;
[cities release];
}
Any pointers (no pun intended) gratefully received.
Cheers