title: where are autorelease pools required in a secondary NSThread runloop?
body: <p>The run loop for the secondary thread of my application is below.
  It has a nested control loops.</p>
  
  <ul>
  <li>The outer loop runs for the duration of the application</li>
  <li>The inner loop runs while one view is open, then the thread waits while the view is not open.</li>
  <li>Passes through the inner loop are short, a fraction of a second.</li>
  </ul>
  
  <p>My code does not knowingly leave any autoreleased objects in unreleased pools, but I do not know what the OS is doing.</p>
  
  <p>In the main thread, cocoa wraps an autorelease pool around every pass through the run loop.<br>
  In this secondary thread, I believe the closest equivalent is a pass through the inner loop.<br></p>
  
  <p>The <strong>inner</strong> autorelease pool wraps each pass through the inner loop. </p>
  
  <p>The <strong>middle</strong> pool wrap around the inner loop, so that objects created and autoreleased at this level are not held until the application terminates.</p>
  
  <p>the <strong>outer</strong> pool wraps the whole runloop.</p>
  
  <p><strong>How can i determine what effect the creation and release of all these pools is having on the  speed of my code. <br>How can I determine whether all three pools are necessary or overkill?</strong></p>
  
  <p><br>
  <br>
  <br></p>
  
  <p>code and explanation:</p>
  
  <pre><code>- (void)processLoop
  {
  
      NSAutoreleasePool * outerPool = [[NSAutoreleasePool alloc] init];
      [processCondition lock];
  
      //outer loop    
      //this loop runs until my application exits
      while (![[NSThread currentThread] isCancelled])    
      {
          NSAutoreleasePool *middlePool = [[NSAutoreleasePool alloc];
          if(processGo)
          {
              //inner loop
              //this loop runs typically for a few seconds
              while (processGo &amp;&amp; ![[NSThread currentThread] isCancelled]) 
              {
                  NSAutoreleasePool *innerPool = [[NSAutoreleasePool alloc]; init];
                  //within inner loop
                  //this takes a fraction of a second
                  [self doSomething];
                  [innerPool release];
              }
              [self tidyThingsUp];
  
          }
          else
          {
              [processCondition wait];
          } 
          [middlePool release];
      }
      [processCondition unlock];
      [outerPool release];
  }
  </code></pre>
  
  <p>the combination of:</p>
  
  <ul>
  <li>an inner while loop</li>
  <li>NSCondition *processCondition</li>
  <li>toggling <code>processGo</code> between <code>YES</code> and <code>NO</code></li>
  </ul>
  
  <p>allows me to stop and start the inner while loop without cancelling the thread. </p>
  
  <pre><code>if (processGo == YES)
  </code></pre>
  
  <p><br>
  execution enters the inner while loop.<br></p>
  
  <p>When the main thread sets</p>
  
  <pre><code>processGo = NO
  </code></pre>
  
  <p>execution leaves the inner while loop and tidys up <br>
  on the next pass of the outer loop, execution hits</p>
  
  <pre><code>[processCondition wait]
  </code></pre>
  
  <p>and waits</p>
  
  <p>if the the main thread resets</p>
  
  <pre><code>processGo == YES
  </code></pre>
  
  <p>and calls</p>
  
  <pre><code>[processCondition wait]
  </code></pre>
  
  <p>execution re-enters the inner loop</p>