首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sqlite3 - stringWithUTF8String泄露了!

sqlite3 - stringWithUTF8String泄露了!
EN

Stack Overflow用户
提问于 2010-06-05 14:56:08
回答 4查看 826关注 0票数 3

如果有人能帮我解决我的漏水问题,我将不胜感激。泄漏发生在: aImage、aCategory、aDescription、category和categories。我在dealloc中释放它们,但显然这还不够:

代码语言:javascript
复制
-(void) readListFromDatabase:(char *) sqlStatement {
    // Setup some globals
    databaseName = @"mydatabase.sql";

    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    // Setup the database object
    sqlite3 *database;

    // Init the categories Array
    categories = [[NSMutableArray alloc] init];

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        // Setup the SQL Statement and compile it for faster access
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                // Read the data from the result row
                aImage = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
                aCategory = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

                // Create a new category object with the data from the database             
                category=[[Category alloc] initWithName:aImage category_name:aCategory description_text:aDescription];

                // Add the category object to the categories Array
                [categories addObject:category];

                [category release];
            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);

}

- (void)dealloc {
    [databaseName release];
    [databasePath release];
    [categories release];
    [aImage release];
    [aCategory release];
    [aDescription release];
    [category release];


    [super dealloc];
}
EN

回答 4

Stack Overflow用户

发布于 2010-06-05 15:46:40

如果多次调用该方法,则字符串将泄漏,因为您需要释放以前的值。您还会在dealloc中过度释放字符串,因为您从未保留过它们。你应该像这样写作业:

代码语言:javascript
复制
[aImage release];
aImage = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)] retain];

这些字符串可能泄漏的唯一另一种方式是,如果您从线程调用此方法,并且您没有创建自动释放池。

如果从新线程调用该方法,则需要一个自动释放池:

代码语言:javascript
复制
- (void)myThreadFunction {
    NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
    try {
        // ...
        [self readListFromDatabase:whatever];
        // ...
    } @finally {
        [pool release];
    }

}
票数 2
EN

Stack Overflow用户

发布于 2010-06-05 17:23:09

您发布的方法是否在同一对象上被多次调用?如果是,则第一次调用的类别将泄漏,因为每次调用readListFromDatabase:时都会覆盖该类别。尝试:

代码语言:javascript
复制
// Init the categories Array
[categories release];
categories = [[NSMutableArray alloc] init];
票数 1
EN

Stack Overflow用户

发布于 2010-06-05 18:15:51

当我在循环中包含aImage自动释放时,为什么应用程序终止(如果是aImage释放,它也终止)?

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

https://stackoverflow.com/questions/2979479

复制
相关文章

相似问题

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