3

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

Community
  • 1
  • 1
Coder
  • 1,661
  • 4
  • 27
  • 50
  • are you want to specific file remove from the ARC – Nimit Parekh Oct 30 '12 at 06:34
  • Thanks for ur immediate reply. I think u didn't get my question. Why ARC will not take care of releasing objects that are created in other threads. – Coder Oct 30 '12 at 06:36
  • ARC works for objective - c only. & an app can also contain objects other than objective - c. That's why you have found ARC not working with other threads. It's ur illusion only. Actually ARC takes care of all threads that contains object of objective - conly. – Niru Mukund Shah Oct 30 '12 at 06:43
  • 2
    @ForamMukundShah if you think so, I can only advise you do one thing: **do read** ARC's documentation before writing things that are downright wrong. –  Oct 30 '12 at 06:48
  • @H2CO3 I did face such kinda problem. & I've read the document. That's why I wrote this. & FYI the name itself suggest it could be either **Objective-C - Automatic Reference Counter** . or **Objective-C++ - Automatic Reference Counter** – Niru Mukund Shah Oct 30 '12 at 06:56

2 Answers2

3

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.

  • ARC is only set up on the main thread? Can you explain in detail? What part of code comes under main thread? – Coder Oct 30 '12 at 06:48
  • @jithen The one that is executed from `main()` and its ancestors directly, without creating a new thread - i. e. all UI code. More precisely, all code which is not called uaing `pthread_create()`, `NSThread` and `dispatch_async()` with a queue other than the main queue. –  Oct 30 '12 at 06:49
  • I have just created an ARC enable project and started a thread. And in the new thread i have allocated an object and not freed it. So if i do analyze in Xcode, according to you it should throw potential leak. But it is not the case, it is not throwing any potential leak – Coder Oct 30 '12 at 07:21
  • code snippet: I placed this code in ViewDidload method pThread = [[NSThread alloc]initWithTarget:self selector:@selector(myMain) object:nil]; [pThread start]; Now i allocated memory for a string in myMain function. If this part of thread is not covered under ARC, then it must throw potential leak whereas it is not throwing. – Coder Oct 30 '12 at 07:23
  • @jithen memory leaks do not throw an exception, they're just there. –  Oct 30 '12 at 07:30
0

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.

Alex Terente
  • 12,006
  • 5
  • 51
  • 71