iphone: memory leak while reading data in loop fro

2019-08-03 04:50发布

问题:

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

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;             

    }
}

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

回答1:

If your loop is running for a huge number of times then use autorelease pool

loop {
NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc] init];

...code goes here...

[innerPool release];

}

It might help you prevent memory leak



回答2:

got the solution leak is here

[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]

handle this and we are done