2012-09-03 30 views
0

在我的應用程序,我加載一系列圖片來我UIImageView已在init方法創建像的UIImageView內存泄漏

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

} 

但這個代碼會導致內存泄漏.Something像

-malloc - 7KB - imageIO - png_malloc

我已經嘗試過「[[UIImage alloc] initWithContentsOfFile]」,但仍然沒有運氣。

我在這裏錯過了什麼。

大約全班

- (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]; 
} 
+0

只啓用ARC。它爲你解決所有的內存管理。那很簡單。 – apple16

+0

我將創建一個分支並嘗試使用ARC。非常感謝 –

+2

我敢打賭,如果你運行分析器,它會指出你的錯誤。 –

回答

1

代碼不泄漏。您的泄漏位於其他地方:)

泄漏位於init方法中。看這句話:

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

這行創建的形象圖您擁有因爲你使用的init方法。然後,您將其分配給您的財產,它再次擁有它,因爲我猜你的財產是retain。你需要釋放的ImageView您已經將其分配後,你的財產,是這樣的:

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

PS你並不需要這一行

[self.imageView setImage:nil ]; 
+0

當我與模擬器配置文件,它不顯示我泄漏,但它在設備上。 當我得到「UiImage * tempImage = [self getImage:currentIndex]」但沒有將其設置爲imagevew,它不顯示內存泄漏。也許你是對的,Profiler –

+0

中的錯誤不要測試模擬器上的泄漏 - 它使用不同的內存模型,所以你不會得到正確的結果。始終信任設備。但是,您的問題中的代碼仍然沒有任何泄漏 - 您的泄漏是讓您的'UIImage'懸而未決的其他內容。可能是'UIImageView',我認爲你不會在你的'dealloc'中釋放。 – deanWombourne

+0

好的。模擬器的好處。 我正在發佈delloc上的UIImageView。感謝您的檢查。 我更新Ÿ問題與全班 下面的代碼是探查 截圖 http://i46.tinypic.com/vq4d3p.png –