iphone:从数据库循环读取数据时内存泄漏

时间:2011-06-04 08:24:29

标签: iphone memory memory-leaks

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

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正在泄漏,我已经尝试了下面提到的所有解决方案但没有工作 Thanx提前

2 个答案:

答案 0 :(得分:3)

如果你的循环运行了很多次,那么使用自动释放池

  循环{
      NSAutoreleasePool * innerPool = [[NSAutoreleasePool alloc] init];

...code goes here...

[innerPool release];
     

}

它可能有助于防止内存泄漏

答案 1 :(得分:0)

解决方案泄漏在这里

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

处理这个问题,我们已经完成了