views:

274

answers:

7

I am looking for good explanations out there. I have a 1000 pages book about objective-c, but unfortunately the part about memory management, retain counting, is described pretty bad and hard to understand.

+3  A: 

This is one of the most concise good explanations I've found:

http://www.dikant.de/2007/08/23/cocoa-memory-management-101

Adam Alexander
+1  A: 

http://stackoverflow.com/questions/106627/memory-management-in-objective-c http://stackoverflow.com/questions/370427/learn-obj-c-memory-management

They've got some simple answers, but also links to Apple's documentation.

Georg
+5  A: 

A quick explaination: Anything you alloc, new, retain or copy you must also release. Also, anything you DON'T alloc or copy you should retain if you plan on holding on to it beyond the current message loop (or you risk it being autoreleased and becoming an invalid reference).

Eric Petroelje
...and rememeber that anything starting init... follows these rules. initWithImage initWithoutClosingTheFridgeDoor etc.You should also follow these rules when implementing classes. init... messages should return an object with a retain count of one and so on.
Roger Nolan
This is not quite correct. From the Apple documentation http://developer.apple.com/mac/library/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH: **You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message.**
Florin
Eric, I think you mean *alloc*, *retain* or *copy*. :)
h4xxr
+2  A: 

CocoaDev always has pretty good resources and their intro to memory management is no exception.

drewh
+1  A: 

Of course there are plenty of free resources for learning about memory management, but if you're really new to the topic and would prefer a visual approach, Steve Scott (the guy who runs the Mac Developer Network) has a great, soup-to-nuts set of training videos that you can download for USD $10: http://www.mac-developer-network.com/videotraining/beginner/vid001/. It's basically a lecture that goes beyond "using release and autorelease" and talks about how the OS manages memory. Very helpful if you missed this stuff in Comp. Sci. 101.

FWIW, I used a set of three "If" conditions that were helpful when I was starting out (e.g., "If I allocate or copy and object, then..."). It was easier for me to remember these three conditions at first than the complete set of rules. I wrote up a short tutorial that uses these "brain triggers" to explain the rules in case it's helpful to anyone else. http://www.clintharris.net/2009/three-brain-triggers-for-objc-memory-mgmt/

Clint Harris
A: 

Specifically for memory management around use of NIB's and IBOutlet, this is the shortest and clearest description I have seen:

http://blog.airsource.co.uk/index.php/2008/12/23/memory-management-and-nibs/

Kendall Helmstetter Gelner
A: 

For me Apple's Memory Management Programming Guide worked quite well.

It explains right at the beginning the basics so you can start working. But it also goes into more advanced subjects if you are interested.

Florin