简体   繁体   中英

Memory Leak related to setImage

I have a memory leak which crashes the app. When i comment line above, i don't have any memory leak.

myCell.image1.image = image;

myCell is a custom UICollectionViewCell and created as:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
             cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CustomCollectionCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"project" forIndexPath:indexPath]; 

    NSData *imageData = [NSData dataWithContentsOfFile:_images[row] options:0 error:NULL];
    UIImage *image = [UIImage imageWithData:imageData];
    myCell.image1.image = image;
    return myCell;
} 

The image is about 8 MB and there are 7 images. When I open the view controller for the forth time, the app crashes (8x7x4 > 200 MB) ;)

CustomCollectionCell.h

#import <UIKit/UIKit.h>
#import "CellButton.h"

@interface CustomCollectionCell : UICollectionViewCell

@property (strong, nonatomic) IBOutlet UILabel *titlePro;
@property (strong, nonatomic) IBOutlet UILabel *datePro;
@property (strong, nonatomic) IBOutlet UIImageView *image1;
@property (strong, nonatomic) IBOutlet CellButton * button;

@end

CustomCollectionCell.m

#import "CustomCollectionCell.h"
#import "UIView+Themes.h"

@implementation CustomCollectionCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code
    }
    return self;
}


@end

I profiled and Live Bytes get higher than 200 MB, and the app crashes without errors. When i comment the myCell.image1.image = image; . The app doesn't crash.

I'm using ARC. Do you have any ideas?

The view controller that contains CustomCollectionCell had a modal segue and i never called the line below.

[self dismissViewControllerAnimated:YES completion:nil];

That was why my images were not released.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM