1
votes

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?

2
Small correction: You say "it is my job to deallocate the memory." It is your job to release the memory. Whether this results in it being deallocated or not is a separate question.Chuck

2 Answers

2
votes

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.

0
votes

You pretty much answered all your questions yourself correctly.