使用ARC的UIImageView内存泄漏

时间:2012-05-22 02:44:13

标签: ios memory-leaks

在DetailViewController.h中:@property (weak, nonatomic) IBOutlet UIImageView *recipeImage;

在DetailViewController.m中

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.recipeTitle.text = rec.title;
    self.recipeDetail.text = rec.details;
    NSString *fileName = [NSString stringWithFormat: @"%@/%@", [[NSBundle mainBundle] resourcePath], rec.image];
    UIImage *tmp = [[UIImage alloc] initWithContentsOfFile: fileName];
    self.recipeImage.image = tmp;
    NSLog(@"%@", rec.image);
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    recipeTitle = nil;
    recipeDetail = nil;
    recipeImage = nil;
    rec = nil;
}

由于某种原因,recipeImage UIImageView IBOutlet导致内存泄漏

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

嗯。我有点意外,因为我没有看到你的代码中有任何明显的泄漏。所以我做了一个小测试项目(Xcode 4.3.2,iPhone模拟器5.1,ARC,故事板),看看我是否可以重现你的问题并通过分析器运行它,并且没有发现泄漏。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.label1.text = @"Line 1";
    self.label2.text = @"Line 2";

    NSString *filename = [NSString stringWithFormat: @"%@/%@", [[NSBundle mainBundle] resourcePath], @"IMG_0999.PNG"];

    UIImage *image = [[UIImage alloc] initWithContentsOfFile:filename];

    self.image1.image = image;
}
和你一样,我使用了弱属性:

@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UIImageView *image1;

我不认为API超出泄漏范围(例如,我确实在Twitter API中看到了一些泄漏),但我无法重现您的问题。

让我觉得有一些逻辑可能性(这是一个延伸):首先,你的PNG会有什么独特之处吗?也许与我们分享,我可以在我的测试中尝试。或许您可以尝试其他一些PNG文件。或者也许你可以做我做的事情,这是一个超级简单的项目,试图重现泄漏,看看你是否在测试项目中遇到同样的问题。其次,泄密可能是你代码中的其他地方吗?我知道这些与malloc相关的泄漏对于指向相关代码并不好,所以我想知道你的泄漏是否可能来自其他地方。

底线,我试图重现你的问题,但不能。看起来可能需要进一步诊断。

<强>更新

图像文件格式非常复杂,如果你查看PNG description,你会看到iOS算法可能泄漏的十几个潜在区域(由于iOS逻辑中的错误或某些问题)使用PNG文件)。显然,它不应该泄漏,但显然iOS确实预期或正确处理了一些排列。似乎值得http://bugreport.apple.com。无论如何,我建议在您选择的图形编辑器中打开并重新保存PNG文件。有一个可能解决它的外部机会。

相关问题