Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 10-14-2008, 10:15 AM   #6 (permalink)
rames44
Mobile Geek
 
Join Date: Aug 2008
Location: Florida, USA
Posts: 365
Send a message via AIM to rames44 Send a message via Yahoo to rames44
Default

The "rule" basically says that you have to release the object if
(a) you were the one to call "alloc"
(b) it was created using a method named "create"

Otherwise, you should not, unless you explicitly called "retain".

What's happening is that the imageWithContentsOfFile: routine builds the image for you, but (since it doesn't have "create" in its name) it also calls "autorelease" on the image for you. This will cause the autorelease pool to call "release" on the returned object when this particular event is over. If you call "release" manually, then the later "release" from the autorelease pool is one too many.

Thus, an object built this way has an "effective" retain count of zero. (If you ask the object for its retain count, it will report "1", but you have to factor in the pending "release" call from the autorelease pool.) Your assignment to [self setImage:image] presumably adds a "retain", so at the end of the event processing, the autorelease pool's "release" doesn't deallocate the object. With your extra "release", however, you've "undone" the retain in your "setImage" method, and thus the "release" in the autorelease pool deallocates the image. "self" is left with a pointer to a dead object.

I hope that's not too twisty to follow.

A slightly more detailed description can be found here:
Very simple rules for memory management in Cocoa
rames44 is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 158,782
Threads: 89,204
Posts: 380,594
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jameswilliards
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:47 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.