【发布时间】:2011-01-30 03:48:12
【问题描述】:
只有当 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];
问题依然存在,当你开始滚动表格视图时发生泄漏。
我实际上尝试过其他编码,似乎只有 NSASCIIStringEncoding 有效(没有内存泄漏)
如果我在关闭视图之前没有滚动表格视图,则根本没有泄漏。实际上 listOfInfo 本身并没有泄漏,因为当我删除该行时不会发生泄漏
textLabel1.text = [tmpDict objectForKey:@"name"];
任何想法/解决方法如何摆脱这个问题?
注意:我确实有
for (id theKey in listOfInfo) {
[[listOfInfo objectForKey:theKey] release];
}
[listOfInfo release];
已经在dealloc上
【问题讨论】:
-
那里有很多代码!...如果问题的原因是编码类型,我会感到惊讶。现在检查您的代码以尝试找出真正的问题...
-
我也很惊讶。我花了整整一周的时间来解决这个问题,幸运的是,当我将编码更改为 ASCII 时,我发现问题完全消失了。
-
来自苹果的回复:不幸的是,这是亚洲字体的一个已知问题。目前还没有解决此问题的 ETA。如果您还没有这样做,那么您绝对应该在此记录一个新错误,以便让工程人员意识到它正在影响许多开发人员。此外,我将在 7263420 中添加注释,强调紧急性并引用此事件。
-
这可能是模拟器与实际设备的问题。稍后会确认(本周末不在我的开发盒上),但同时在实际设备上尝试配置文件。
标签: iphone memory-leaks uitableview sqlite nsstring