【问题标题】:iphone: strange leakiphone:奇怪的泄漏
【发布时间】:2011-09-08 11:37:14
【问题描述】:

谁能帮我弄清楚为什么这段代码会泄漏,我们该如何处理?

sqlite3 *database;
if (pickerList) {
    self.pickerList=nil;
    [pickerList release];

}
self.pickerList=[[NSMutableArray alloc] init];


NSString *dbPath = [self applicationDocumentsDirectory];

dbPath=[dbPath stringByAppendingPathComponent:@"database"];
dbPath=[dbPath stringByAppendingPathComponent:@"OFFENDERSDB.sqlite"];



if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

    if (isAlertForViolationPicker) {


        const char *sqlStatement = "SELECT * FROM VIOLATIONS_TBL";

        sqlite3_stmt *compiledStatement;

        if (sqlite3_prepare(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

            while (sqlite3_step(compiledStatement) == SQLITE_ROW) {

                NSString *recSTR=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

                [self.pickerList addObject:recSTR];
                [recSTR release];
                recSTR=nil;             

            }
        }
        //[tempRowArray release];
        sqlite3_finalize(compiledStatement);
        //sqlite3_reset(compiledStatement);
        sqlite3_close(database);

    }
    else {

        const char *sqlStatement = "SELECT * FROM PLAN_TBL";
        sqlite3_stmt *compiledStatement;

        if (sqlite3_prepare(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

            while (sqlite3_step(compiledStatement) == SQLITE_ROW) {


                NSString *recSTR=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];

                [self.pickerList addObject:recSTR];
                [recSTR release];
                recSTR=nil;         

            }
        }
        sqlite3_finalize(compiledStatement);
        sqlite3_close(database);

    }






}

sqlite3_reset(compiledStatement);

recSTR 在这种情况下会泄漏,我已经尝试了下面提到的所有解决方案,但都没有奏效(更新了代码) 提前感谢

【问题讨论】:

  • 你没有在这里泄露recSTR。泄漏很可能与字符串参与pickerList 有关。这段代码还不够搞清楚。
  • Instruments 只告诉您泄漏对象的创建位置,而不是泄漏对象的位置。
  • 在构建设置中打开该死的静态分析器!

标签: iphone memory-leaks


【解决方案1】:

看起来您可能正在泄漏pickerList。您有一个指向 pickerList 的指针,然后将其设置为 nil。然后你向这个点发送一个释放消息(这实际上是一个无操作)。如果你使用:

if (pickerList)
{
     [pickerList release];
     self.pickerList=nil;
}

而不是您当前的代码,您的表现会更好吗?没有看到更多代码很难说,但您肯定想在将 ivar 设置为 nil 之前发布。 (这就是说,如果你已经完成了 @property (retain) UIPickerList *pickerList 那么 self.pickerList = nil 将释放pickerList。如果你这样做了,那么你的 [pickerList release] 调用是多余的。)

您很可能会收到有关从仪器中泄漏的 recSTR 的报告。但这并不意味着问题不在于pickerList。查看代码,recSTR 不太可能由一个挂起的 pickerList 实例拥有,因为您已经丢弃了指向它的指针,然后向 nil 发送了释放消息。所以你最终会泄漏 recSTR 和 pickerList。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-18
    • 2012-07-06
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    相关资源
    最近更新 更多