Why is this memory leak line?

Any ideas why the line below would be a memory leak?

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

inside the cellForRowAtIndexPath method:

- (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;
}

And here is the corresponding method from person.m

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

EDIT: added init method:

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

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

    return self;
}
+3
source share
5 answers

The only thing I can think of here that might cause a leak is that you can save imageURL in your person class and not have a release in your dealloc method. Therefore, when you release a person, he skips the imageURL property.

+2
source

Try dividing the line and retesting. This may give you some idea.

NSURL *imgURL = person.imageURL;
NSData *imgData = [NSData dataWithContentsOfURL:imgURL]
cell.imageView.image = [UIImage imageWithData: imgData];

, , .

, ? URL?

+1

UIImage . , , UIImage .

+1

dealloc Person?

, , dealloc:

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

.

[NSData dataWithContentsOfURL:...] Cocoa . [[[NSData alloc] initWithContentsOfURL:...] autorelease]. - .

0

Source: https://habr.com/ru/post/1717282/


All Articles