If you are like me (a C/C++ programmer who has to write small amounts of Objective C for a certain mobile platform), this document contains pretty much all of the answers you need regarding the question "am I leaking memory?"
I'll try to limit the ranting - but there are definitely some surprising things here, particularly if you are used to a more traditional reference-counted environment. Key points:
- Reference counting is not automatic like Java, so you absolutely can screw it up.
- You are expected to not create cyclic reference-count dependencies - see "weak vs. strong" references.
- Apple's naming conventions are very consistent, once you understand them. This is important because the rules for memory allocation vary in a way that make the APIs more convenient, but less consistent. (That is, sometimes objects are retained or auto-retained for you. Lacking automatic reference counting of everything in the language, this is necessary to keep code from coming completely bloated. But it means that you, the programmer, have to be able to look at an Obj-C method and go "I [don't] need to retain" and get the answer right 100% of the time.
- autorelease basically acts as a deferred release, similar to a deferred-destroyer idiom in C++.
Two points:
ReplyDeleteFirst, the retain/release memory management is a feature of Cocoa and Cocoa Touch, not the Objective-C language itself.
Second, the rules are rather simpler than you realize. If you +alloc, -copy or -retain, you must -release or -autorelease. If you didn't alloc, copy or retain an object, getting rid of it is not your responsibility.
If you care whether an object remains valid after the method you're in currently, send it a -retain message. If you don't care, don't retain it.
-jcr
It depends on the version you're using. As you mentioned, you're only developing for the iPhone SDK. If you develop a normal Cocoa app you can turn on Objective-C 2.0 support, which includes a garbage collector that makes retain/release unnecessary along with a lot of other goodies.
ReplyDeleteJust for the sake of being obscenely precise: Objective-C 2.0 is used for CocoaTouch as well. Garbage collection is not a language feature, not in Obj-C and not in any other language -- it's a feature of that particular language's runtime or libraries.
ReplyDeleteIn short: Cocoa on OS X has automatic garbage collection. CocoaTouch on iPhone and iPod Touch does not.
I do agree that Obj-C is weird though :-).
I agree that Cocoa/Objective C is very weird, but IMO not because of the reasons you mentioned. Most of these rules apply to C++ as well (no automatic garbage collection, having to think about ownership, ownership transferred in a few cases like new, clone, copy, whatever).
ReplyDeleteIf you are going to rant about something, then I would rant about the weird + - prefixes (I always forget after one hour what they mean), the odd header/interface declaration structure, all the boilerplate code you need to create in every constructor, ...