Leaks while adding to array in while loop
I have function named: - (void) AddSortedCustomFeed :(NSMutableArray*) rssList;
this function adds items(Articles) from sqlite database to NSMutableArray
here how this function works:
- (void) AddSortedCustomFeed :(NSMutableArray*)rssList {
NSLog(@"\n\n\n ----- Add Sorted SQL Database -----");
NSLog(@"Start");
// Create Query String.
NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT mainLink, title, summary, pubDate, author, imageLink, body, favorites, pubdatetime FROM ARTICLES WHERE customfeed = 'Y' ORDER BY pubdatetime DESC"];
NSLog(@"Query String is: %@", sqliteQuery);
// Pointer to Article and Statement.
Article* article;
sqlite3_stmt* statement;
// Prepare SQL for work.
if( sqlite3_prepare_v2(articlesDB, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK ) {
// Get next row from database.
while( sqlite3_step(statement) == SQLITE_ROW ) {
// Alloc and init article.
article = [[Article alloc] initWithValues:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 1)]
mainLink:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 0)]
summary:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 2)]
pubDate:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 3)]
author:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 4)]
imageLink:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 5)] ];
//article.body = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 6)];
NSString* favo = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 7)];
开发者_运维问答 article.favorite = [favo hasPrefix:@"N"] ? NO : YES;
// Add to list.
[rssList addObject:article];
// Release article.
[article release];
}
}
else NSLog( @"SortSQLDatabase: Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(articlesDB) );
// Finalize and close database.
sqlite3_finalize(statement);
NSLog(@"End\n\n\n");
}
How you can see in this function I create article Article* article;
and in while loop alloc and initialize it. After that I add Article object to NSMutableArray
and then release it, but then I call [article release];
witch must call delloc function it doesn't calls ? I cant understand why. I try different ways but all my tries crashes. This is article about Article Class - link
dealloc isn't called on the Article object because [rssList addObject:article]
retains the article object. So your retain count is 1, and it won't be released. Now if you release rssList (or remove the article object from the array), the retain count will hit 0, and the object will be released.
P.S: if you have a while loop like this I recommend you add an autorelease pool to avoid autorelease objects build up.
while( .. )
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// .. your code .. //
[pool release];
}
P.P.S: if you hit 'Shift+Cmd+A' XCode will statically analyze your code and look for leaks/memory mishaps. I have a feeling it will find some.
You have a few choices, you should experiment to see what you want to do. Basically, once you have sent [article release];
you will need to start from the beginning to make the article
. So you could do something like
Article *article = [[Article alloc] initWithValues:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 1)]
mainLink:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 0)]
summary:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 2)]
pubDate:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 3)]
author:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 4)]
imageLink:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 5)] ];
and leave [article release];
where it is now. Be sure to take out your line of Article* article;
from above though. This is probably lots of overhead that you don't want.
You could keep everything the same and move [article release];
down so that is it outside of the loop.
精彩评论