title: ObjC: BAD ACCESS when call blocks ^{} in later functions?
body: <p>Following this <a href="https://stackoverflow.com/questions/11277745/objc-put-logic-within-a-variable-and-queue-that-variable-in-an-array-daydre">discussion</a>, I've encountered a bad access issue;</p>
  
  <p>A loop has several steps: a, b, c, ... x, y, z:</p>
  
  <pre><code>-(void)cycle:(float)delta{
  [self stepA]
  [self stepB]
  // etc.
  [self stepZ]
  }
  </code></pre>
  
  <p>At some point, step x does the following:</p>
  
  <pre><code>// IRQ is an NSMutableArray
  // Self is a reference to the engine running the cycles
  [IRQ addObject:^{ NSLog(@"hello! %@", self); } ];
  </code></pre>
  
  <p>Later, step z is to process all "delayed" calls:</p>
  
  <pre><code>            for (int i = 0; i &lt; [IRQ count]; i++){
                  void (^delayedCall)(void) = [IRQ objectAtIndex:i];
                  delayedCall();
              }
  
              [IRQ removeAllObjects];
  </code></pre>
  
  <p>Result: EXEC_BAD_ACCESS</p>
  
  <p>Now, if step x only adds a plain string with no object reference like follows, step Z works fine:</p>
  
  <pre><code>[IRQ addObject:^{ NSLog(@"hello!"); } ];
  </code></pre>
  
  <p>Last observation, if a same step both adds blocks to the queue AND iterates over the queue to execute the blocks, then no problem occurs.
  Like the reference to an object gets "lost" as the step: method is left?</p>
  
  <p>I don't understand much in this area and will need more help!</p>
  
  <p>edit:
  James, just tried the following to avoid that reference cyle:</p>
  
  <pre><code>NSString *userName = @"James";
  [IRQ addObject:^{ NSLog(@"hello %@", userName); } ];
  </code></pre>
  
  <p>and it also happens. How would your solution apply to this?</p>
  
  <p>Thanks in advance!</p>