我正在开发一个应用程序,其中我使用数据库操作。我写在数据库类中的方法如下。内存问题?
-(NSMutableArray *)getData: (NSString *)dbPath{
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK){
NSString *sqlQuery = [NSString stringWithFormat:@"SELECT empID, addText FROM Employee WHERE nameID = %@", nameID];
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, [sqlQuery UTF8String], -1, &selectstmt, NULL) == SQLITE_OK){
while (sqlite3_step(selectstmt) == SQLITE_ROW){
[dataArray addObject:[[NSMutableDictionary alloc] init]];
[[dataArray lastObject] setObject:[NSString
stringWithFormat:@"%d", sqlite3_column_int(selectstmt, 0)] forKey:@"empID"];
[[dataArray lastObject] setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)] forKey:@"addText"];
}
}
sqlite3_finalize(selectstmt);
}
sqlite3_close(database);
return dataArray;
}
上述代码在模拟器上正常工作,但无法在设备上正常工作。 我也在追踪内存泄漏,其中我在上面的方法代码中发现内存泄漏。但我无法解决那个内存泄漏问题。
现在我也在下面的方法中找出内存泄漏。
- (ID)initWithString:(的NSString *)STR属性:(的NSDictionary *)属性
{ 如果((个体= [超级INIT])) { _buffer = [STR mutableCopy ]。 _attributes = [NSMutableArray arrayWithObjects:[ZAttributeRun attributeRunWithIndex:0 attributes:attributes],nil]; } return self; }
_buffer = [str mutableCopy];附近的泄漏。并且泄漏跟踪让我在输出中连续增加NSCFString字符串分配。我如何维护它?
在此先感谢。
return [dataArray autorelease];我之前使用过的声明,但它没有奏效。首先它显示员工数据,当我从员工表中检索另一行时,它会给出“_kill”错误。 – Tirth 2010-09-17 13:14:38
Thanks.NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];[dict setObject:[NSString stringWithFormat:@“%d”,sqlite3_column_int(selectstmt,0)] forKey:@“empID”];[dict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)] forKey:@“addText”]; [dataArray addObject:dict]; [字典发布]; 上面的存根很好地工作。 – Tirth 2010-09-17 13:35:58