简体   繁体   中英

UIImage imageNamed memory leak

I am trying to understand why I have memory leaks in a very basic implementation of a UIImage and a UIImageView.

I am not using ARC in that case (correctly disabled).

My code is pretty straightforward :

UIImage *image = [UIImage imageNamed:@"my_image.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[[self view] addSubview:imageView];
[imageView release];

I am implementing this code in the viewDidLoad: method of a UIViewController.

By calling the method imageNamed: of UIImage, I know that I will get an object that I do not own / an autorelease object. This object will also be retained by the UIImageView object instantiated just after. So the only object I have the ownership is the UIImageView one.

After running this app with the Memory Leaks Instruments, I have this report :

在此输入图像描述

I heard about the cache system that operates but I should not have have memory leaks because some datas are cached.

Here is a reference to the answer with the cache explanation : https://stackoverflow.com/a/2930567/1154501

Thank you in advance !

Edit : Tried also with ARC, and I got the same issue.

[UIImage imageNamed:] is managed by the operating system. Deallocating the UIImage created from this method might free up the memory that was allocated. If you have lots of images or user generated content, you should be using [UIImage imageWithContentsOfFile:] or [UIImage imageWithData:] .

If you create too many images with [UIImage imageNamed:], your app may get killed by iOS because of memory usage. I made a sample app to prove this to myself, see more here: iOS UIImage storage formats, memory usage and encoding / decoding

Did you try to open the 'Extended Detail' right panel and look for the exact line where the memory is leaking?

Your code is OK, my opinion is that the leak is somewhere else.

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