0

Consider we are implementing our own thread with lot of autoreleased object. Is it mandatory to use autorelease pool on this scenario if yes/no why?

Ankit Vyas
  • 7,507
  • 13
  • 56
  • 89

2 Answers2

4

It's mandatory to have an autorelease pool on any thread that you create, because Cocoa internals expect there to be one in place and you will leak memory if it isn't there.

Cocoa always expects there to be an autorelease pool available. If a pool is not available, autoreleased objects do not get released and your application leaks memory. If you send an autorelease message when a pool is not available, Cocoa logs a suitable error message.


Applications that link in Objective-C frameworks typically must create at least one autorelease pool in each of their threads.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Well, it's not _quite_ mandatory. It is perfectly defined what will happen: objects leak, and messages get logged. While that isn't usually what you want, that doesn't mean it couldn't possibly be what you want… – abarnert Jun 14 '12 at 21:27
3

It is mandatory even with a single autoreleased object, because otherwise it will leak.

Manlio
  • 10,768
  • 9
  • 50
  • 79