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

标签 iphone objective-c memory-leaks

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

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/

相关文章:

ios - 处理 UIButtons iOS

c# - 如果 .NET 中的 MemoryStream 未关闭,是否会产生内存泄漏?

ios - 应用商店的销售和趋势问题

iphone - 更新主线程上的标签不起作用

ios - Cordova iOS 启动 comgooglemaps 失败

objective-c - -initWithContentsOfFile : for an NSMutableArray

ios - UIView 显示顺序问题?

mysql - 与其他 DBMS 相比,MySQL 是否消耗更多资源?

c++ - 如何使用valgrind?

ios - 使用keyWindow的Xcode Swift iPhone 5、5s崩溃错误