iphone: memory leak while reading data in loop fro

2019-08-03 04:48发布

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

2条回答
三岁会撩人
2楼-- · 2019-08-03 05:17

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

查看更多
闹够了就滚
3楼-- · 2019-08-03 05:21

got the solution leak is here

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

handle this and we are done

查看更多
登录 后发表回答