1

我有一个名为的函数:- (void) AddSortedCustomFeed :(NSMutableArray*) rssList;这​​个函数将 sqlite 数据库中的项目(文章)添加到NSMutableArray这里这个函数是如何工作的:

- (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");
}

您如何在这个函数中看到我创建文章Article* article;和在 while 循环中分配并初始化它。之后我将 Article 对象添加到NSMutableArray然后释放它,但是我调用[article release];了女巫必须调用它不调用的 delloc 函数?我不明白为什么。我尝试不同的方式,但我所有的尝试都崩溃了。这是关于文章类的文章-链接

4

2 回答 2

2

[rssList addObject:article] 因为保留了 article 对象,所以不会在 Article 对象上调用 dealloc 。所以你的保留计数是 1,它不会被释放。现在如果你释放 rssList(或从数组中删除文章对象),保留计数将达到 0,并且该对象将被释放。

PS:如果您有这样的 while 循环,我建议您添加一个自动释放池以避免自动释放对象堆积。

while( .. )
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  // .. your code .. //
  [pool release];
}

PPS:如果您点击“Shift+Cmd+A”,XCode 将静态分析您的代码并查找泄漏/内存事故。我有一种感觉,它会找到一些。

于 2011-04-27T12:56:01.243 回答
0

你有几个选择,你应该尝试看看你想做什么。基本上,一旦你发送[article release];了,你需要从头开始制作article. 所以你可以做类似的事情

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 release];它现在的位置。一定Article* article;要从上面取出你的线。这可能是您不想要的大量开销。

你可以保持一切不变并向[article release];下移动,这样它就在循环之外。

于 2011-04-27T12:54:02.160 回答