在while循环中添加到数组时泄漏 [英] Leaks while adding to array in while loop

查看:142
本文介绍了在while循环中添加到数组时泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为:- (void) AddSortedCustomFeed :(NSMutableArray*) rssList;的函数,此函数将sqlite数据库中的项目(文章)添加到NSMutableArray,此处此函数的工作原理:

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

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

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不在Article对象上调用,因为[rssList addObject:article] 保留该article对象.因此您的保留数为1,并且不会被释放.现在,如果释放rssList(或从数组中删除商品对象),则保留计数将变为0,并且该对象将被释放.

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:如果您有类似while的循环,建议您添加一个自动释放池,以避免自动释放对象的建立.

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];
}

PPS:如果您按下"Shift + Cmd + A",则XCode将静态分析您的代码并查找泄漏/内存故障.我觉得它会找到一些.

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.

这篇关于在while循环中添加到数组时泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆