문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

You pretty much answered all your questions yourself correctly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top