Question

I've finally decided to stop beating around the bush and teach myself some Objective-C. It's all making sense, except when I get to the memory management and this idea of the "autorelease pool". From my understanding, if I specifically create an object using init,new, alloc or tell it to retain, then it is my job to deallocate the memory. However, an autorelease pool will automatically free up any objects associated with the innermost autorelease pool? For example, say I do this:

TestOBJ* foo = [[[TestOBJ alloc] innit] autorelease];

Then it will be released at the end of the first @autoreleasepool block, or whenever the first [autoreleasepool drain] is called?

Also; just because an object is created within an autoreleasepool block doesn't mean it is automatically labeled as autorelease, correct?

Was it helpful?

Solution

Correct for both points.

Objects only get added to autorelease pools if you call -autorelease on them. By convention all methods not starting with alloc, new, copy or mutableCopy return objects that they have themselves added to the autorelease pool, so you don't have to do it yourself.

See http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html for details.

OTHER TIPS

You pretty much answered all your questions yourself correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top