gpt4 book ai didi

iphone - 为什么这条线会泄漏内存?

转载 作者:搜寻专家 更新时间:2023-10-30 20:04:52 25 4
gpt4 key购买 nike

知道为什么下面的行会泄漏内存吗?

cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:person.imageURL]];

在 cellForRowAtIndexPath 方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell"];

if (cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"PersonCell"] autorelease];
}
Person *person = [[Person alloc] initWithUserName:[people objectAtIndex:indexPath.row]];

cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:person.imageURL]];

cell.textLabel.text = [people objectAtIndex:indexPath.row];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

[person release];

return cell;
}

这里是person.m的相关方法

- (NSURL*) imageURL
{
return [NSURL URLWithString:[userInfo valueForKey:@"profile_image_url"]];
}

编辑:添加初始化方法:

- (id)initWithUserName:(NSString *)user{

userName = [user copy];
userInfo = [[TwitterHelper fetchInfoForUsername:userName] retain];
updates = [[TwitterHelper fetchTimelineForUsername:userName] retain];

return self;
}

最佳答案

我在这里唯一能想到的可能导致泄漏的是你可能在你的 person 类中保留了 imageURL 而没有在它的 dealloc 方法中释放。所以当你释放 person 时,它会泄露 imageURL 属性。

关于iphone - 为什么这条线会泄漏内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1401278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com