Hi, guys! i'm newbie in iphone development. I have problems with the memory leak. I have such code.
do {
int s = sqlite3_step(statement);
switch (s) {
case SQLITE_ROW:{
Article *a = [[[Article alloc] init] autorelease];
for (int i = 0; i < columnCount; i++) {
const char *columnName = sqlite3_column_name(statement, i);
if(strncmp(columnName, "title", strlen("title")) == 0){
const char* colStr = (char*)sqlite3_column_text(statement, i);
if(colStr != NULL)
a.title = [[[NSString alloc] initWithCString: colStr encoding:NSUTF8StringEncoding] autorelease];
continue;
}
if(开发者_运维百科strncmp(columnName, "author", strlen("author")) == 0){
const char* colStr = (char*)sqlite3_column_text(statement, i);
if(colStr != NULL)
a.author = [[[NSString alloc] initWithCString:colStr encoding:NSUTF8StringEncoding] autorelease];
continue;
}
if(strncmp(columnName, "description", strlen("description")) == 0){
const char* colStr = (char*)sqlite3_column_text(statement, i);
if(colStr != NULL)
a.description = [[[NSString alloc] initWithCString:colStr encoding:NSUTF8StringEncoding] autorelease];
continue;
}
if(strncmp(columnName, "link", strlen("link")) == 0){
const char* colStr = (char*)sqlite3_column_text(statement, i);
if (colStr)
a.link = [[[NSString alloc] initWithCString:colStr encoding:NSUTF8StringEncoding] autorelease];
continue;
}
if(strncmp(columnName, "imageUrl", strlen("imageUrl")) == 0){
const char* colStr = (char*)sqlite3_column_text(statement, i);
if (colStr)
a.imageUrl = [[[NSString alloc] initWithCString:colStr encoding:NSUTF8StringEncoding] autorelease];
continue;
}
if(strncmp(columnName, "pubDate", strlen("pubDate")) == 0){
const char* colStr = (char*)sqlite3_column_text(statement, i);
if(colStr)
a.pubDate = [[[NSString alloc] initWithCString:colStr encoding:NSUTF8StringEncoding] autorelease];
continue;
}
}
[array insertObject:a atIndex:0];
}
break;
case SQLITE_DONE:
sqlite3_finalize(statement);
dataForReadingAvailable = NO;
break;
default:{
NSLog(@"getArticlesForFeed:sqlite3_step failed.Error:%s",sqlErrMsg);
return nil;
}
break;
}
}while(dataForReadingAvailable);
Tools from Xcode shows that i have leak memory when i a'm allocated object and when i initialized it properties. But why it's happens. All objects is autorealeased, so i think that will not to be such situation. Thanks.
I think perhaps you overwrite the allocated memory with the new pointer somewhere.
I would replace
Article *a = [[[Article alloc] init] autorelease];
with
Article *a = [[Article alloc] init];
//some code
[a release]; //when you don't need it anymore
This is not a good practice to use autorelease much. It can even slow down your app btw in case of many objects. And the strings like
a.title = [[[NSString alloc] initWithCString: colStr encoding:NSUTF8StringEncoding] autorelease];
I would replace with
a.title = [NSString stringWithCString:colStr encoding:NSUTF8StringEncoding];
In this case you also rely on autorelease pool, but it's much simpler.
EDIT: Indeed, your 'Article' allocation is in the do-while loop. In string [array insertObject:a atIndex:0]; your aray retains object and you don't need it anymore, but you rely on autorelease. Analyzer (or what tool you're using) sees, that you placed allocation in the cycle's body without releasing it after each iteration. This way, on the second iteration of the loop, you overwrite your "a" object, loosing the old pointer and leaking memory this way (pool won't find this pointer, since "a" overwritten). Even if you pass your loop only once, the analyzer does not care and gives you a warning.
精彩评论