iphone:奇怪的泄漏

时间:2011-06-04 18:51:03

标签: iphone memory-leaks

任何人都可以帮我解释为什么这段代码泄漏了,我们该如何处理呢?

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在这种情况下泄漏了,我已经尝试了下面提到的所有解决方案,但都没有工作(更新了代码) Thanx提前

1 个答案:

答案 0 :(得分:1)

看起来好像你可能正在泄漏pickerList。你有一个指向pickerList的指针,然后你将其设置为nil。然后你发送一个发布消息到这一点(实际上是一个no-op)。如果您使用:

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

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

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