Leaks while adding to array in while loop

早过忘川 提交于 2019-12-07 08:18:26

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!