只有在NSString不使用NSASCIIStringEncoding进行编码时,我的UITableView才会出现严重的内存泄漏问题。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cell";
UILabel *textLabel1;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
textLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(105, 6, 192, 22)];
textLabel1.tag = 1;
textLabel1.textColor = [UIColor whiteColor];
textLabel1.backgroundColor = [UIColor blackColor];
textLabel1.numberOfLines = 1;
textLabel1.adjustsFontSizeToFitWidth = NO;
[textLabel1 setFont:[UIFont boldSystemFontOfSize:19]];
[cell.contentView addSubview:textLabel1];
[textLabel1 release];
} else {
textLabel1 = (UILabel *)[cell.contentView viewWithTag:1];
}
NSDictionary *tmpDict = [listOfInfo objectForKey:[NSString stringWithFormat:@"%@",indexPath.row]];
textLabel1.text = [tmpDict objectForKey:@"name"];
return cell;
}
-(void) readDatabase {
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",myDB]];
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char sqlStatement = [[NSString stringWithFormat:@"select id,name from %@ order by orderid",myTable] UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSString *tmpid = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *tmpname = [NSString stringWithCString:(const char *)sqlite3_column_text(compiledStatement, 1) encoding:NSUTF8StringEncoding];
[listOfInfo setObject:[[NSMutableDictionary alloc] init] forKey:tmpid];
[[listOfInfo objectForKey:tmpid] setObject:[NSString stringWithFormat:@"%@", tmpname] forKey:@"name"];
}
}
sqlite3_finalize(compiledStatement);
debugNSLog(@"sqlite closing");
}
sqlite3_close(database);
}
当我更改行
NSString *tmpname = [NSString stringWithCString:(const char *)sqlite3_column_text(compiledStatement, 1) encoding:NSUTF8StringEncoding];
到
NSString *tmpname = [NSString stringWithCString:(const char *)sqlite3_column_text(compiledStatement, 1) encoding:NSASCIIStringEncoding];
内存泄漏消失了
我试过NSString stringWithUTF8String但它仍然泄漏。 我也试过了:
NSData *dtmpname = [NSData dataWithBytes:sqlite3_column_blob(compiledStatement, 1) length:sqlite3_column_bytes(compiledStatement, 1)];
NSString *tmpname = [[[NSString alloc] initWithData:dtmpname encoding:NSUTF8StringEncoding] autorelease];
问题仍然存在,当你开始滚动tableview时会发生泄漏。
我实际上尝试过其他编码,似乎只有NSASCIIStringEncoding有效(没有内存泄漏)
如果我在关闭视图之前没有滚动tableview,它根本就没有泄漏。实际上listOfInfo本身并没有泄漏,因为当我删除行时泄漏永远不会发生
textLabel1.text = [tmpDict objectForKey:@"name"];
任何想法/解决方法如何摆脱这个问题?
注意:我确实有
for (id theKey in listOfInfo) {
[[listOfInfo objectForKey:theKey] release];
}
[listOfInfo release];
on dealloc已经
答案 0 :(得分:3)
程序中的泄漏实际上就在这里:
[listOfInfo setObject:[[NSMutableDictionary alloc] init] forKey:tmpid];
您正在分配字典并且永远不会释放它,因此它会泄漏。你应该写:
[listOfInfo setObject:[[[NSMutableDictionary alloc] init] autorelease] forKey:tmpid];
或
[listOfInfo setObject:[NSMutableDictionary dictionary] forKey:tmpid];
由于此字典包含您的字符串,因此字符串会与字典一起泄漏。更改为NSASCIIStringEncoding
并不能解决此漏洞,它可能只是伪装它(因为字符串需要随后进行转换而Leak可能无法将其跟踪到新位置)或转换可能完全失败(如果字符串无法转换为ASCII)。