 |
 |
|
 |
03-10-2009, 07:34 PM
|
#1 (permalink)
|
|
New Member
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
|
Memory Management 101
Since so many questions come up on this topic I thought I would give a quick summary on the basic rules of iPhone memory management. More specifically about when you should release an object.
Here's the simplest rule: If you directly call any of the following methods on an object then you must call release on the object:
alloc/init
retain
copy
mutableCopy
If you write a method that needs to return an object created with one of the above methods then your return value should look something like this:
Code:
return [result autorelease];
This allows your method to fulfill its obligation to release as required but the release won't happen until a little later so the code that calls this method has an opportunity to retain the result if required.
|
|
|
03-10-2009, 07:36 PM
|
#2 (permalink)
|
|
Tutorial Author
Join Date: Oct 2008
Location: Ontario, Canada
Posts: 459
|
Thank you!
|
|
|
03-11-2009, 01:56 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 440
|
Quote:
Originally Posted by RickMaddy
Since so many questions come up on this topic I thought I would give a quick summary on the basic rules of iPhone memory management.
|
Great concise summary Rick. Thanks. Much easier for newbies to digest than the Apple Introduction to Memory Management Programming Guide for Cocoa, which is where I had been going. Even the name is long. :P
|
|
|
04-06-2009, 06:36 PM
|
#4 (permalink)
|
|
Registered Member
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 440
|
Quote:
Originally Posted by RickMaddy
Here's the simplest rule: If you directly call any of the following methods on an object then you must call release on the object:
alloc/init
retain
copy
mutableCopy
|
I have a couple simple follow-up questions: - Do you need to release statements when you "initWithFormat", "initWithTitle", or "SoundEngine_Initialize"
- How do you decide when to release within a method (e.g. [actionSheet release]; ) vs. doing it in the - (void)dealloc method
Last edited by DenVog; 04-06-2009 at 07:19 PM.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 509 |
| 56 members and 453 guests |
| aoredsson, beausejour, bensj, BustinStones, chinhminhdo, CHV, codeMonkey, conanjb, CoolApps, Corey, CunningCat, dany88, dda, DorkyMohr, dre, ggalante, healthyutech, iPhoneDevelopment, Jeremy1026, john3478, jorgmart, JoshuaCaputo, katyaskr, KennyChong, kkrimmer, LoDani, LunarMoon, macoholic, Maximilian, mlo, MMan, Mr Jack, mriphoneman, MrMattMac, Nightmare_82, not_too_shabby, NSeven, opetopic, pablo.roqueta, pablox, pierotofy, rarindeed, Rudy, ryguy2503, salva, seriessix, sgraves, stubbsjoe, supudo, Tambourin, themathminister, virvalid, Vivek Nirkhe, warmi, wuf810, ZunePod |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 21,495
Threads: 35,777
Posts: 156,729
Top Poster: smasher (2,448)
|
| Welcome to our newest member, AnuSambath |
|