开发者

iphone: strange leak

Can anybody help me out why this piece of code is leaking and how can we handle it?

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(compiledSta开发者_开发百科tement);

recSTR is leaking in this case and I have tried all the below mentioned solutions but none worked (updated the code) Thanx in advance


It looks as though you may be leaking pickerList. You have a pointer to pickerList which you then set to nil. Then you send a release message to this point (which is in effect a no-op). If you use:

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

instead of your current code, do you fare any better? Without seeing more code it's hard to say, but you definitely want to release before you set the ivar to nil. (This said if you you've done @property (retain) UIPickerList *pickerList then self.pickerList = nil will release pickerList. If you've done this then your [pickerList release] call is redundant.)

You may well get a report of a recSTR leaking from instruments. But that doesn't mean that the issue isn't with pickerList. Looking at the code, it would not unlikely that recSTR is owned by an instance of pickerList that is hanging around because you've discarded the pointer to it and then sent a release message to nil. So you'll end up with a leak of recSTR and pickerList.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜