views:

329

answers:

2

Let's say I have an object called Person. Person internally loaded an UIImage called "person.png".

In my app i create many Person objects, creating and even deleting them at runtime.

Since a Person object internally has a retained reference to UIImage, does this mean each Person has a unique memory allocation for the image?

Or do all Person objects point to the same memory location for the image?

+1  A: 

If your Person instance uses retain for its reference to a UIImage instance, then it is pointing to one instance of that image somewhere in memory.

If you use copy instead of retain, you will have a unique instance of UIImage.

Consider taking a look at the Apple reference on memory management.

Alex Reynolds
+2  A: 

Well, it depends on how you're getting the UIImage.

Are you using +[UIImage imageNamed:] each time you create a new Person object? If so, UIKit caches the image and returns references to a single copy in memory. You retain it to make sure it stays loaded, then release it when you're done referencing it, but only one UIImage object is created in memory.

If, on the other hand, you're calling +[UIImage initWithData:] or [[UIImage alloc] initWithContentsOfFile:] you're creating a new copy in memory each time (and you probably need to stop that :)

Jon Gotow
Right, I am definitely using imageNamed.
AlvinfromDiaspar
Then you're fine - just remember to retain/release as appropriate. And if you're loading lots of images with imageNamed, be aware that under iPhone OS 2.0 those cached images aren't unloaded even when you're done with them. Some people have run out of memory when loading lots of large images, though this is reportedly fixed in iPhone OS 3.
Jon Gotow