views:

688

answers:

4

Don't shoot me, I know this must have been asked a thousand times...

I am not comfortable with the lack of good documentation on Objective-C memory. I understand alloc, dealloc, retain, release and all that but there is some confusion left in my head.

Is it just lazy programming or does Objective-C do some 'behind the scenes' automagic activity in regard to allocating memory?

Coming from a C background (centuries ago), I know that pointers are just pointers... you also have to reserve space for what the pointer points to or you will start stomping on your own program and other variables.

The code examples I find (and that are in the books I read - all of which are sadly out of date with the current version of XCode and Interface Builder) never seem to allocate storage space for some objects such as NSString. They declare a pointer (eg. NSString *aString;) then start assigning text to the string. No allocation of memory for the string is every invoked!?!? So are all these examples just lazy code looking for a place to crash?

Also, books talk about declaring 'pool' memory and that it is automagically inserted into your code. When I create projects, classes and objects, no such code is to be found anywhere. Has Apple done away with this automatic insertion or is it something that happens during compile time?

Is there a penultimate reference book or website that will explain all this once and for all?

+12  A: 

You should read the Memory Management Programming Guide for Cocoa.

notnoop
I would +10 this if I could. That is the ultimate resource for Cocoa memory management.
Chuck
+1 Seriously, it's all in here.
Abizern
A: 

There are probably two points that you missed:

  1. Factory methods such as [NSString stringWIthString:theString] internally do the equivalent of [[[NSString alloc] initWithString:theString] autorelease] (or possibly literally that). autorelease interacts with the NSAutoReleasePool to which you referred, basically attempting to release the object at the next event loop cycle.
  2. All Objective-C objects are pointers. Rather than using malloc or new directly, Objective-C objects which inherit from NSObject (the root class in Apple's Objective-C libraries and indeed in most Objective-C code) use +(id)alloc in place of the direct memory management calls. Again, internally this probably uses malloc to do the allocation.
dcrosta
A: 

Objective-C 2.0 has a garbage collector.

Yes NSString *aString; will do some magic behind the scenes stuff for you. If you wanted to control memory, you could use something like:

NSString* aString = [[NSString alloc] init];

Have you read "Learn Objective C on the Mac?" It's a little out of date, but not my much. The main differences are in XCode.

Aaron
I recommend learning memory management and not relying on garbage collection. iPhone environment doesn't support garbage collection.
notnoop
What? Declaring an NSString pointer variable does not do anything magical behind the scenes.
Chuck
Thanks everyone.I found two references which I will study:Memory Management Programming Guide for Core Foundation (as mention in one of the responses)http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.htmlMemory Management Programming Guide for Cocoahttp://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.htmlYes, I have the 'Cocoa Programming for Mac OS X' but it is the second edition. It helped kick start me but I more or less gave up on it due to some major omissions in the sample code.
Steve
+2  A: 

I think the best reference available for understanding memory management in the Objective-C language is the The Objective-C 2.0 Programming Language available as a PDF from Apple at the following URL:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf

Chapter 3 Allocating and Initializing Objects speaks to memory management.

A less detailed but more concise discussion of memory management in the Objective-C language can be found at Very simple rules for memory management in Cocoa found in the URL below:

http://www.stepwise.com/Articles/Technical/2001-03-11.01.html

Your specific question concerning NSString allocation rules are covered in that article under the title Retention Count rules.

"Objects created using convenience constructors (e.g. NSString's stringWithString) are considered autoreleased."

Another good book for understanding Cocoa programming in general that has many examples and explanations is Cocoa Programming for Mac OS X (3rd Edition) available from Amazon here:

http://www.amazon.com/exec/obidos/ASIN/0321503619/bignerdranch-20

The author Aaron Hillegass has been programming in Objective-C since the Next days and now teaches Cocoa programming for a living. I own the book, but I don't have it on me so I can't tell you the specific chapter to look at right now. However, I can assure you that it is a good book.

I hope the above references are helpful for your understanding.

John Scipione
The link changed to http://www.stepwise.com.sharedcopy.com/Articles/Technical/b57ca071a8e1e903044f18fa51db58d4.html for stepwise.com
Sijo