UIImage imageNamed and release responsibility...?

I'm new to objective-c and to iphone development. I'm going through the samples and I've run across something confusing and seemingly in contradiction to the Cocoa Object Ownership Policy. Maybe I'm just missing something though. A sample project called MoveMe in the "iPhone Dev Center sample code" library has the code in question.

MoveMe has a UIView subclass called PlacardView. This view needs to make use of an image in the nib. It gets a reference to that image by invoking [UIImage imageNamed:]. Here is the confusing bit. It never calls retain on the returned image, but it does release it in PlacardView's dealloc. Cocoa's object ownership policy make me believe the sample should have retained this object if it was going to release it in the future. Is this release an error, or do they know something I don't?

Thanks in advance for any help.

Side note:
Even more confusing is the fact that it the view assigns the image DIRECTLY to one of its declared (retain) properties, circumventing the retain which would have been implicitly called if it invoked "self.property =".

Also, I did inspect the object at runtime and it does have a retainCount of 1 when returned from imageNamed, but I'd expect that to be the case since UIImage has the responsibility for maintaining its own cache...?

iPhone OS 3.0

Posted on Jun 22, 2009 11:37 AM

Reply
2 replies

Jun 23, 2009 12:13 AM in response to bitsculptor

bitsculptor wrote:
... I've run across something confusing... MoveMe has a UIView subclass called PlacardView. This view needs to make use of an image in the nib. It gets a reference to that image by invoking \[UIImage imageNamed:\]. Here is the confusing bit. It never calls retain on the returned image, but it does release it...

I agree this is confusing if not incorrect. The step I don't understand is the release in dealloc. I couldn't find anyplace where the UIImage object was retained, and since the object isn't owned by Placard View it shouldn't be released.

Have you tried to release the UIImage prior to dealloc to see if there was any ill effect?

It wouldn't surprise me if neither retain nor release had any effect on a UIImage obtained with imageNamed. Consider the following under imageNamed in the +UIImage Class Reference+:
This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

The implication is that the system caches UIImage objects, imageNamed returns a pointer to one of those objects, but that pointer can't be used to alter the object in the cache. Are we supposed to be smart enough to infer from the above that the return from imageNamed doesn't need to be retained? It certainly wouldn't make much sense to autorelease a system cache object just because the program referenced it.

I searched elsewhere, e.g. the +Memory Management Programming Guide for Cocoa+ and the +Resource Programming Guide+. If there's any guidance on this topic I think it would be in one of those docs, but I couldn't find any clues. Please feel free to see if you can find something I missed.

Sorry I can't give you a straight answer. Hopefully someone who knows more about Cocoa will show up. I hope my thoughts on your question are helpful in some way.

Jun 23, 2009 7:47 AM in response to RayNewbie

Thanks for the response. It must just be a bug in the sample. An extremely confusing bug for someone new to cocoa/objective-c. 🙂

I tried releasing the UIImage early as you suggested, and the application does crash next time it attempts to render using that image. I'm glad that is the case, otherwise it would mean UIImage did something even more confusing like no-op the release.

Thanks again.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

UIImage imageNamed and release responsibility...?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.