1

In Apple API Reference about NSAutoReleasePool https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html It point that "The Application Kit creates an autorelease pool on the main thread at the beginning of every cycle of the event loop".

It puzzle me. I have three question about this.

1) What's mean "The Application Kit"? UIKit? If "The Application Kit" mean UIKit, then, I create an application which don't use UIkit. Will it create a autoreleasePool on the main thread at the beginning of every cycle of the event loop.

2) Is "On the main thread" mean that On others thread (Not mainThread) will break this rule?

3) Is "event loop" mean runloop ?

# #

Add: I write a demo to test it. when I create a thread, will create an autoreleasePool by runloop, automatically?

enter image description here enter image description here

Then, In Application MainThread, Create a taskThread. enter image description here

The result: enter image description here

So, the question 2)Is "On the main thread" mean that On others thread (Not mainThread) will break this rule? In this demo, It's not. The second thread every runloop create an autoreleasePool automatically. Because the autoreleaseObject's dealloc method is called.

Is there any wrong in my demo? It puzzle me.

1 Answers1

1
  1. You're looking at the Mac documentation. AppKit is the Macintosh version of UIKit. Both have pretty much the same logic with regards autorelease pools
  2. Yes. A new thread won't automatically have an autorelease pool or an event loop. Having said that, most of the time you won't be creating new threads yourself. There are things like GCD and NSOperation that do much of what you need without the manual stuff
  3. I think for this purposes of this, yes
Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • 1. Will it create a autoreleasePool on the main thread at the beginning of every cycle of the event loop which is not base on UIKit? 2. I write a demo to test it. I create a thread without autoreleasePool. In this thread, I create an autorelease Object. When the thread is over, the autorelease Object dealloc called. Why? – Sakuragi Hanamichi Jan 14 '14 at 02:16
  • 1. No, because the run loop is a property of UIKit/AppKit. There's no saying that other frameworks will have one. 2. "Applications that link in Objective-C frameworks typically must create at least one autorelease pool in each of their threads" https://developer.apple.com/library/ios/documentation/cocoa/conceptual/Multithreading/CreatingThreads/CreatingThreads.html – Stephen Darlington Jan 14 '14 at 08:15
  • 1. Is Runloop a property of UIKit? I don't think so. Each thread have One and only one runloop which automaticlly created for it as needed. 2. I write that demo want to test wheather second thread every runloop will create autoreleasePool, Only. – Sakuragi Hanamichi Jan 14 '14 at 09:40
  • 1. By property I don't mean something you can access by dot notation. I mean "something it does for you." 2. In your sample code you create an autorelease pool -- that's what @autoreleasepool does. – Stephen Darlington Jan 14 '14 at 12:08
  • Also, when you create the runloop in the thread, you call CFRunLoopRun(). The CF prefix means "Core Foundation." This is the framework that underlies both AppKit and UIKit. So it's a property, a function, a part of -- however you want to phrase it -- the application framework. – Stephen Darlington Jan 14 '14 at 12:10
  • Thanks for your answer. But, I still puzzled. As you seen. Before the thread autoreleasePool drain, the autorelease object MyObject has been dealloc. That means, there is an autorelease pool in every runloop cycle. – Sakuragi Hanamichi Jan 14 '14 at 12:15
  • Or the run loop drains the autorelease pool at the end of each iteration. – Stephen Darlington Jan 14 '14 at 12:30
  • @StephenDarlington You can only drain an autorelease pool once. It's the same as `-release` but when using GC, it forces garbage collection instead. – JeremyP Jan 14 '14 at 14:28
  • Yes, you're right about draining the pool. It seems that run loops have their own [autorelease pool](https://mikeash.com/pyblog/friday-qa-2010-01-01-nsrunloop-internals.html) and that's what's drained at the end of your loop. – Stephen Darlington Jan 14 '14 at 19:33
  • @StephenDarlington Yes.But in the documentation, "The Application Kit creates an autorelease pool on the main thread at the beginning of every cycle of the event loop". This say "on the main thread". I am not sure that is same rule in other threads? – Sakuragi Hanamichi Jan 15 '14 at 01:37
  • AppKit _does not_ create run loops or autorelease pools on anything but the main thread. But if you create one yourself -- as you do in your sample code -- then the behaviour is obviously the same. That is, in your sample code you will get an autorelease pool that is drained automatically every iteration "for free." – Stephen Darlington Jan 15 '14 at 07:24