using autorelease in ARC enabled projects [duplicate]
-
15-07-2021 - |
質問
Possible Duplicate:
ARC memory leaks
Can we use @autorelease pool in ARC enabled projects. If so, what is the use of using @autorelease pool. I found in google that, even if the project is ARC enabled, it will be taking care of only main thread. If we create other threads, then we have to take care of releasing objects. Can anyone explain
解決
When you migrate from manual memory management to ARC you will replace the:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[pool release];
With
@autoreleasepool {
}
Read the NSAutoreleasePool docs and Advanced Memory Management Programming Guide for a better understanding.
他のヒント
Yes, ARC is only set up on the main thread by default. However, you can also take advantage of it inside background threads. The solution is simple: the semantics of the @autoreleasepool { }
is different for ARC; it doesn't actually allocate and release an NSAutoreleasePool object the classic MRC way, it simply tells ARC to take care of the objects in the tread in which it was called as well, achieving an autorelease pool-style effect.