首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iPhone -数据库读取方法和内存泄漏

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

Stack Overflow用户
提问于 2010-04-20 23:27:01
回答 3查看 963关注 0票数 0

在我的应用程序中,一个RSS阅读器,我得到了内存泄漏,我无法修复,因为我不能理解它们来自哪里。以下是Instruments指出的代码。

代码语言:javascript
运行
复制
-(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匹配这些使用:

代码语言:javascript
运行
复制
[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, X)];

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

EN

回答 3

Stack Overflow用户

发布于 2010-04-20 23:35:59

你泄露文章是因为:

代码语言:javascript
运行
复制
article = nil;
[article release];

为什么你要尝试释放nil?

只需删除行设置为nil的文章,它是不需要的。根据上面的示例,该代码中可能存在更多问题。

票数 1
EN

Stack Overflow用户

发布于 2010-06-09 08:58:39

我只是花了几天的时间来追踪这个问题,并在下面为我解决了这个问题。

将dealloc方法添加到您的文章NSObject并释放所有字符串。然后在您的readArticlesFromDatabase方法中,首先发布文章,然后执行初始化。

票数 1
EN

Stack Overflow用户

发布于 2010-04-26 19:26:24

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

我的错误是将“文章”数组初始化到我的函数中。实际上,每次我调用我的函数时,我都会丢失这个数组中的前一个数据。现在,我在启动时初始化了“文章”数组,并简单地在我的函数中删除了它的内容:

代码语言:javascript
运行
复制
- (void)applicationDidFinishLaunching:(UIApplication *)application {
       // My Code
       articles = [[NSMutableArray alloc] init];
       // My Code
}

然后

代码语言:javascript
运行
复制
-(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);
}

现在我要关注的是由文章removeAllObjects引起的“文章”泄漏。如果我不删除所有的对象,我不会有任何泄漏,但如果我删除了,我就会有泄漏。有什么想法吗?如何才能在不泄漏这些文章对象的情况下清空数组?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2676385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档