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

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

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 01-23-2009, 01:49 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 22
Default When to release, when to retain?

Hello,

I've been programming for a bit over a month.

To this day, I still struggle with the memory management in objective-c. I understand reference counting, but I'm confused about which functions retains and which auto releases etc.

What are some general rules to abide by?

For example, if I call an objective-c function, how do I know I should release explicitly or whether it's auto released?

Thank you.
adamk77 is offline   Reply With Quote
Old 01-23-2009, 01:56 PM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 495
Default

There are much more in-depth discussions on the matter, but in a nutshell, you only need to release anything that you "own". You generally own any *objects* that you send a message that contains "alloc" or "copy".

Objects created with convenience constructors are auto-released. Generally at the end of the current method.

Code:
// you own this one, need to release
NSString *myString = [[NSString alloc] initWithFormat:@"Hello %@", @"world"];

// convenience constructor, will be auto-released
NSString *myString = [NSString stringWithFormat:@"Hello %@", @"world"];
__________________
My Apps on AppStore : gScale (guitar scales reference), eMaze, eMaze Lite, eTimesheet
exorcyze is offline   Reply With Quote
Old 01-23-2009, 02:21 PM   #3 (permalink)
Senior Member
 
Join Date: Jan 2009
Location: Florida, USA
Posts: 471
Default

Apple has a very good and detailed memory mangement guide on the developer site that should answer all your questions.
Slayer5150 is offline   Reply With Quote
Old 01-23-2009, 03:18 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: Northeast United States
Posts: 135
Send a message via AIM to MattjDrake Send a message via Skype™ to MattjDrake
Default

This is probably one of the hardest things to get used to on the iPhone.

It helps to keep in mind that you must release something after you have used "alloc" when your create an object. This is usually used with "init" to create instances of an object. What gets confusing is that some classes have functions that do the same thing.

For example, check out the difference between these two ways to create a string:

NSString *string1 = [[NSString alloc] initWithString:@"needs to be released"];

NSString *string2 = [NSString stringWithString:@"no need to release"];

Both these things do the same thing but you do not need to worry about releasing the second one because you can assume that it is "autoreleased". It is a sort of unspoken rule that situations like string2 above are autoreleased while string1 will need to be explicitly released.

At any rate, this is clearly a complex issue. What I used to help me deal with this problem was that I bought a video (think it was about 10 bucks) from the Mac Developer Network that tells you everything you need to know about memory management.

Mac Developer Network » Blog Archive » Memory Management in Objective-C

They do a much better job of dealing with this problem then the Apple documentation does and they have some tips on how to cope with memory management. Highly recommend it.
__________________
How To Make iPhone Apps
http://howtomakeiphoneapps.com
Cocoa-Touch, iPad & iPhone Programming Videos, Tutorials & How-To exclusively for new app developers
MattjDrake is offline   Reply With Quote
Old 01-23-2009, 03:22 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 22
Default

Thanks for your answers everyone.

I was browsing the forum and found an excellent tutorial written by someone on this forum.

Getters, setters and properties for the newbie

It answered my questions very well.
adamk77 is offline   Reply With Quote
Old 01-23-2009, 03:30 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 429
Default

Finally an explanation for that weird property/synthesize stuff! thanks so much for this find! (and of course thanks to the actual author)
lbendlin 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


» Advertisements
» Stats
Members: 41,861
Threads: 49,770
Posts: 213,057
Top Poster: BrianSlick (3,139)
Welcome to our newest member, melodizzzy
Powered by vBadvanced CMPS v3.1.0

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