仅在设备上的仪器中有很多奇怪的泄漏(模拟器中没有泄漏)
我开始使用仪器并发现了很多泄漏。我不知道如何解决它们。
仪器显示我的这条线有泄漏:
NSArray *topLevelObjects = [[NSArray alloc] initWithArray:[[NSBundle mainBundle] loadNibNamed:@"SearchResultsTableViewCell" owner:self options:nil]];
这有什么问题吗?
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SearchResultsTableViewCell";
SearchResultsTableViewCell *cell = (SearchResultsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSArray alloc] initWithArray:[[NSBundle mainBundle]
loadNibNamed:@"SearchResultsTableViewCell"
owner:self options:nil]];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (SearchResultsTableViewCell *) currentObject;
break;
}
}
[topLevelObjects release], topLevelObjects = nil ;
}
Product *product = [productMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = product.title;
cell.detailTextLabel.text = product.desc1;
UIImageView *imageView = nil;
if (product.photo == nil) {
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ph38x38.jpg"]];
} else {
imageView = [[UIImageView alloc] initWithImage:product.photo];
}
imageView.frame = CGRectMake(10., 3., 38., 38.);
[cell addSubview:imageView];
[imageView release];
return cell;
}
我还有很多其他泄漏,但 Instruments 甚至没有在代码中显示行,例如有一个泄漏: GenerlaBlock-64 - UIKit - GetContextStack 我该如何解决?我应该去哪里寻找它?
我寻找了某种教程,但所有这些都只显示了保留计数、分配、释放的简单示例
I started working with instruments and have a lot of leaks. I don't have an idea how to solve them.
Instrument show that i have leak in this line:
NSArray *topLevelObjects = [[NSArray alloc] initWithArray:[[NSBundle mainBundle] loadNibNamed:@"SearchResultsTableViewCell" owner:self options:nil]];
What is wrong with this?
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SearchResultsTableViewCell";
SearchResultsTableViewCell *cell = (SearchResultsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSArray alloc] initWithArray:[[NSBundle mainBundle]
loadNibNamed:@"SearchResultsTableViewCell"
owner:self options:nil]];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (SearchResultsTableViewCell *) currentObject;
break;
}
}
[topLevelObjects release], topLevelObjects = nil ;
}
Product *product = [productMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = product.title;
cell.detailTextLabel.text = product.desc1;
UIImageView *imageView = nil;
if (product.photo == nil) {
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ph38x38.jpg"]];
} else {
imageView = [[UIImageView alloc] initWithImage:product.photo];
}
imageView.frame = CGRectMake(10., 3., 38., 38.);
[cell addSubview:imageView];
[imageView release];
return cell;
}
I also have a lot of other leaks, but instruments doesn't even show line in the code, for example there is a leak:
GenerlaBlock-64 - UIKit - GetContextStack
How can I solve it? where should i look for it?
I looked for some kind of tutorials, but all of them show only trival examples with retain count, alloc, release
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
线程并不是真正的问题。它是 UILabel 的“文本”属性。
此属性只能在主线程上设置。
改为调用performSelectorOnMainThread:
Threads aren't the real problem. It's the "text" property of UILabel.
This property can only be set on the main thread.
Call performSelectorOnMainThread instead:
为什么不删除
initWithArray:
并使用(并且您还必须删除
release
并设置nil
行)// 编辑:尝试设置
owner:nil
Why don't you just remove the
initWithArray:
and use(and you have to remove the
release
and setnil
line too)// EDIT: try to set the
owner:nil
线程造成了所有问题。我一直在进行分叉而不返回主线程。这就是问题所在。
Threads were making all the problems. I was doing forks all the time without returning to main thread. That was the problem.