iphone:奇怪的漏洞
问题描述:
有人可以帮我解释为什么这段代码在泄漏,我们该如何处理它?iphone:奇怪的漏洞
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提前
答
看起来好像你可能会泄漏pickerList。你有一个指向pickerList的指针,然后你将它设置为nil。然后你发送释放消息到这一点(这实际上是一个没有操作)。如果你使用:
if (pickerList)
{
[pickerList release];
self.pickerList=nil;
}
而不是你现在的代码,你会更好吗?没有看到更多的代码很难说,但你肯定想在你把伊娃尔设置为零之前释放。 (这表示如果你已经完成@property(retain)UIPickerList * pickerList then self.pickerList = nil将释放pickerList。如果你已经完成了这个操作,那么你的[pickerList release]调用是多余的。)
你可能以及从仪器泄漏recSTR泄漏的报告。但这并不意味着问题不在pickerList中。查看代码,recSTR不是不可能被pickerList的一个实例所拥有,因为您已经放弃了指向它的指针,然后将释放消息发送给nil。所以你最终会泄漏recSTR和pickerList。
你不会在这里泄漏'recSTR'。漏洞很可能与该字符串涉及'pickerList'有关。这个代码是不够的,以弄清楚。 – 2011-06-04 18:53:25
Double post:http://*.com/questions/6235632/iphone-memory-leak-while-reading-data-in-loop-from-datatbase – dasdom 2011-06-04 18:56:28
仪器只告诉你泄漏对象的创建位置,而不是它在哪里被泄露。 – albertamg 2011-06-04 19:04:25