 |
 |
|
 |
03-10-2009, 06: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, 06:36 PM
|
#2 (permalink)
|
|
Tutorial Author
Join Date: Oct 2008
Location: Ontario, Canada
Posts: 459
|
Thank you!
|
|
|
03-11-2009, 12:56 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 502
|
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, 05:36 PM
|
#4 (permalink)
|
|
Registered Member
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 502
|
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 06: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: 563 |
| 41 members and 522 guests |
| Abdel, Alger Cheng, ankushthemaster, applezinga, arcturus, ashishjraval, azraelx23, blum, chaoz1337, djp_phillips, Duncan C, Error404, foxykirian, fredidf, harrytheshark, hfmn, Hololont, houssem, ideafactory, irishkiwi, jaime1988, jasimsajid, jplus, ledererc, Locker, Lokendra, lokidil, max62niers, milanjansari, mirkosarra, mistergod, mohan.agadkar, momolgtm, myPhone, rambos, SirGeraint, Son of a Beach, Tambourin, telo3, umarmara, _sean |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,350
Threads: 39,142
Posts: 171,612
Top Poster: smasher (2,577)
|
| Welcome to our newest member, 17make |
|