iPhone-数据库读取方法和内存泄漏

iPhone-数据库读取方法和内存泄漏,iphone,database,nsstring,memory-leaks,Iphone,Database,Nsstring,Memory Leaks,在我的应用程序RSS阅读器中,我无法修复内存泄漏,因为我无法理解它们的来源。这是仪器指出的代码 -(void) readArticlesFromDatabase { [self setDatabaseInfo]; sqlite3 *database; articles = [[NSMutableArray alloc] init]; if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

在我的应用程序RSS阅读器中,我无法修复内存泄漏,因为我无法理解它们的来源。这是仪器指出的代码

-(void) readArticlesFromDatabase {



[self setDatabaseInfo];

 sqlite3 *database;

 articles = [[NSMutableArray alloc] init];

 if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
  const char *sqlStatement = "select * from articles";
  if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
   while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

    NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
    NSString *aDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
    NSString *aUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
    NSString *aCategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
    NSString *aAuthor = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
    NSString *aSummary = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
    NSMutableString *aContent = [NSMutableString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)];
    NSString *aNbrComments = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)];
    NSString *aCommentsLink = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)];
    NSString *aPermalink = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 11)];

    [aContent replaceCharactersInRange: [aContent rangeOfString: @"http://www.mywebsite.com/img/action-on.gif"] withString: @"hellocoton-action-on.gif"];
    [aContent replaceCharactersInRange: [aContent rangeOfString: @"hhttp://www.mywebsite.com/img/action-on-h.gif"] withString: @"hellocoton-action-on-h.gif"];
    [aContent replaceCharactersInRange: [aContent rangeOfString: @"hthttp://www.mywebsite.com/img/hellocoton.gif"] withString: @"hellocoton-hellocoton.gif"];

    NSString *imageURLBrut = [self parseArticleForImages:aContent];    
    NSString *imageURLCache = [imageURLBrut stringByReplacingOccurrencesOfString:@":" withString:@"_"];
    imageURLCache = [imageURLCache stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    imageURLCache = [imageURLCache stringByReplacingOccurrencesOfString:@" " withString:@"_"];

    NSString *uniquePath = [tmp stringByAppendingPathComponent: imageURLCache];
    if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath]) {
     imageURLCache = [@"../tmp/" stringByAppendingString: imageURLCache];
     [aContent replaceCharactersInRange: [aContent rangeOfString: imageURLBrut ] withString: imageURLCache];
    }

    Article *article = [[Article alloc] initWithName:aName date:aDate url:aUrl category:aCategory author:aAuthor summary:aSummary content:aContent commentsNbr:aNbrComments commentsLink:aCommentsLink commentsRSS:@"" enclosure:aPermalink enclosure2:@"" enclosure3:@""];

    [articles addObject:article];

    article = nil;
    [article release];
   }
  }
  sqlite3_finalize(compiledStatement);

 }
 sqlite3_close(database);
}
`

我有很多“文章”泄漏和NSString与这些匹配,使用:

[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, X)];

我尝试了很多不同的代码我总是有这些漏洞。有人有办法帮我吗?

你泄露文章是因为:

article = nil;
[article release];
你为什么试图释放零


只需将行设置项目删除为nil,就不需要了。鉴于上面的示例,该代码中可能还有很多问题。

我终于自己找到了部分解决方案

我的错误是将“articles”数组初始化到我的函数中。实际上,每次调用函数时,我都会丢失此数组中以前的数据。 现在,我在启动过程中初始化“articles”数组,并在函数中删除其内容,如下所示:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
       // My Code
       articles = [[NSMutableArray alloc] init];
       // My Code
}
然后

现在我必须关注由[articles removeAllObjects]引起的“Article”泄漏。
如果我不移除所有物体,我就没有任何泄漏,但如果我移除了,我就有泄漏。有什么想法吗?如何清空阵列而不泄漏这些文章对象?

我刚刚花了几天的时间来跟踪这个问题,下面为我解决了这个问题


向对象添加dealloc方法并释放所有字符串。然后在readArticlesFromDatabase方法中,首先发布文章,然后进行初始化。

谢谢您的回答。我试图补充这一点,因为我在这一点上工作了很长一段时间,现在我正在寻求这一点。正如你所说的,问题并不是来自于这一点(根据仪器,仪器(有或没有)不会改变任何东西)。你说代码中还有很多问题,你知道这些行为什么会产生泄漏吗:NSString*aName=[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,1)];感谢您的时间和帮助。删除对nil的分配将停止泄漏文章。您还泄漏了数组“articles”,应该在方法结束时释放该数组。如果你想把它作为一个实例变量,你应该使用一个属性赋值。即使没有“article=nil;”,它仍然在泄漏“article”。关于数组“articles”,我以后会使用它,我需要保留它,所以我在dealloc中释放它。当你说“NSString*aName=[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,1)];”时,必须释放aName。否则会导致内存泄漏。
-(void) readArticlesFromDatabase {

    [self setDatabaseInfo];
    sqlite3 *database;

    [articles removeAllObjects];

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        const char *sqlStatement = "select * from articles";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {

                NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                NSString *aDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
                NSString *aUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
                NSString *aCategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
                NSString *aAuthor = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
                NSString *aSummary = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
                NSMutableString *aContent = [NSMutableString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)];
                NSString *aNbrComments = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 8)];
                NSString *aCommentsLink = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)];
                NSString *aPermalink = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 11)];

                [aContent stringByReplacingOccurrencesOfString: @"http://www.mywebsite.com/img/action-on.gif" withString: @"hellocoton-action-on.gif"];
                [aContent stringByReplacingOccurrencesOfString: @"http://www.mywebsite.com/img/action-on-h.gif" withString: @"hellocoton-action-on-h.gif"];
                [aContent stringByReplacingOccurrencesOfString: @"http://www.mywebsite.com/img/hellocoton.gif" withString: @"hellocoton-hellocoton.gif"];

                NSString *imageURLBrut = [self parseArticleForImages:aContent]; 
                NSString *imageURLCache = [imageURLBrut stringByReplacingOccurrencesOfString:@":" withString:@"_"];
                [imageURLCache stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
                [imageURLCache stringByReplacingOccurrencesOfString:@" " withString:@"_"];

                NSString *uniquePath = [tmp stringByAppendingPathComponent: imageURLCache];
                if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath]) {
                    imageURLCache = [@"../tmp/" stringByAppendingString: imageURLCache];
                    [aContent replaceCharactersInRange: [aContent rangeOfString: imageURLBrut ] withString: imageURLCache];
                }

                Article *article = [[Article alloc] initWithName:aName date:aDate url:aUrl category:aCategory author:aAuthor summary:aSummary content:aContent commentsNbr:aNbrComments commentsLink:aCommentsLink commentsRSS:@"" enclosure:aPermalink enclosure2:@"" enclosure3:@""];

                [articles addObject:article];

                [article release];
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}