Gonzales Gokhan
Gonzales Gokhan

Reputation: 492

UIImageView memory Leak

In my app, I am loading series of images to my UIImageView which has been created on init Method like

- (UIImage *)getImage:(int)currentIndex {
    NSString *path = [self getImagePath:currentIndex];
    [self.imageView setAccessibilityLabel:path]; 

    path=[bundle pathForResource:path ofType:extension];    
    return  [UIImage imageWithContentsOfFile:path] ;

}

- (void)changeImage:(int)currentIndex {

    [self.imageView setImage:nil ];
    [self.imageView setImage:[self getImage:currentIndex]];

}

But this code causes memory leaks .Something like

-malloc - 7KB - imageIO - png_malloc

i have tried "[[UIImage alloc ] initWithContentsOfFile]" with appropriate release calls but still no luck.

What am i missing on here.

Roughly whole class

- (id)initWithFrame:(CGRect)frame andBundle:(NSBundle *) bundleTobeInjected {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    self.opaque = YES;
        bundle=bundleTobeInjected;
        commonFunctions=[[Common alloc] init];
        self.imageView = [[UIImageView alloc] initWithFrame:frame] ;
        [self.imageView setImage:nil ];
        self.imageView.contentMode = self.contentMode;
        [self addSubview:imageView];
        [self setAccessibilityEnabled];
    }
    return self;
}
-(void)setAccessibilityEnabled
{
    self.imageView.isAccessibilityElement=true;
}

#pragma mark - Custom Logic
- (NSString *)getImagePath:(int)currentIndex {

    NSString *path = [NSString stringWithFormat:@"%@%d%@", prefix,currentIndex,[commonFunctions imageNamedSmartForRotationSuffix]];
    NSLog(@"%@", path);
    return path;
}

- (UIImage *)getImage:(int)currentIndex {
    NSString *path = [self getImagePath:currentIndex];
    [self.imageView setAccessibilityLabel:path]; 

    path=[bundle pathForResource:path ofType:extension];    
    return  [UIImage imageWithContentsOfFile:path] ;

}

-(void)changeImage:(int)currentIndex {


    [self.imageView setImage:nil ];
    [self.imageView setImage:[self getImage:currentIndex]];


}


-(void)dealloc{
    [extension release];
    [prefix release];
    [defaultImage release];
    [image release];
    [imageView release];
    [commonFunctions release];
    [super dealloc];
}

Upvotes: 0

Views: 2755

Answers (1)

deanWombourne
deanWombourne

Reputation: 38485

That code doesn't leak. Your leak is somewhere else :)

The leak is in the init method. Look at this line :

self.imageView = [[UIImageView alloc] initWithFrame:frame];

That line creates an image view that you own because you used an init method. You then assign that to your property which owns it again because I'm guessing your property is retain. You need to release the imageView after you've assigned it to your property, like this :

self.imageView = [[[UIImageView alloc] initWithFrame:frame] autorelease];

PS You don't need this line

[self.imageView setImage:nil ];

Upvotes: 1

Related Questions