Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-10-2009, 06:34 PM   #1 (permalink)
New Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
Default 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.
RickMaddy is offline   Reply With Quote
Old 03-10-2009, 06:36 PM   #2 (permalink)
Tutorial Author
 
Steaps's Avatar
 
Join Date: Oct 2008
Location: Ontario, Canada
Posts: 459
Default

Thank you!
Steaps is offline   Reply With Quote
Old 03-11-2009, 12:56 PM   #3 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 502
Default

Quote:
Originally Posted by RickMaddy View Post
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
DenVog is offline   Reply With Quote
Old 04-06-2009, 05:36 PM   #4 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 502
Default

Quote:
Originally Posted by RickMaddy View Post
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:
  1. Do you need to release statements when you "initWithFormat", "initWithTitle", or "SoundEngine_Initialize"
  2. 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.
DenVog is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,350
Threads: 39,142
Posts: 171,612
Top Poster: smasher (2,577)
Welcome to our newest member, 17make
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:24 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0