【问题标题】:Incorrect decrement of the reference count引用计数的错误减少
【发布时间】:2011-09-08 17:38:34
【问题描述】:

文件管理器对象的引用计数减少错误。

-(void) checkDb{
BOOL success;
// Create a FileManager object, we will use this to check the status of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];

// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:dbPath];

if (success)
{
    //we found the file, we need to check version
    sqlite3 *db;
    //NSLog(@"Current Databasepath: %@",dbPath);
    // Open the current db (found in the user's filessytem)
    if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) {
        const char *sql = "select dbversion from settings";
        sqlite3_stmt *rs;
        if(sqlite3_prepare_v2(db,  sql, -1, &rs, NULL) == SQLITE_OK) {
            if (sqlite3_step(rs) == SQLITE_ROW) {
                //not eof
                int curDbVersion=sqlite3_column_int(rs,0);
                if (curDbVersion>=minDbVersion){
                    //good dbversion, no need to copy from resources
                    return;
                }
            }
        }
        sqlite3_finalize(rs);
    }
    sqlite3_close(db);
}

//we reached this section which means:
//either database was not found, or invalid db version
//so, we need to copy it from the resources directory (or maybe download it from internet?)

// Get the path to the database in the application package
NSString *dbPathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbName];

// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:dbPathFromApp toPath:dbPath error:nil];

[fileManager release];

}

程序运行良好,但是当我分析它时,我收到了警告。

这是我收到的警告的屏幕截图:

我可能错过了什么提示?

【问题讨论】:

    标签: xcode ios debugging memory-leaks


    【解决方案1】:

    注意创建文件管理器指针所在的行:

    NSFileManager *fileManager = [NSFileManager defaultManager];
    

    找不到copynewallocretain 等词:您不拥有文件管理器,因此您不应释放它。

    你的最后一行:

    [fileManager release];
    

    正在有效地尝试释放defaultFileManager,您当然不拥有它。

    【讨论】:

    • 非常感谢,现在我对它的理解更好了.. 我从 2 周开始就开始了 ios 开发.. 并且正在挖掘它.. 干杯..
    【解决方案2】:

    不要release文件管理器——它是一个局部变量,稍后会自动释放。

    【讨论】:

    • +1 表示“不要释放文件管理器”,但原因与变量的范围没有任何关系。只是代码没有分配、复制、保留文件管理器,因此没有业务发布。
    猜你喜欢
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多