title: EXC_BAD_ACCESS when using recursive block
body: <p>I'm trying to create recursion using blocks. It works for a while, but eventually it crashes and gives me a bad access exception. This is my code:</p>
  
  <pre><code>BOOL (^Block)(Square *square, NSMutableArray *processedSquares) = ^(Square *square, NSMutableArray *processedSquares) {
      [processedSquares addObject:square];
  
      if (square.nuked) {
          return YES; // Found a nuked square, immediately return
      }
  
      for (Square *adjacentSquare in square.adjacentSquares) {
          if ([processedSquares containsObject:adjacentSquare]) {
              continue; // Prevent infinite recursion
          }
  
          if (Block(adjacentSquare, processedSquares)) {
              return YES;
          }
      }
  
      return NO;
  };
  
  __block NSMutableArray *processedSquares = [NSMutableArray array];
  BOOL foundNukedSquare = Block(square, processedSquares);
  </code></pre>
  
  <p>Explanation: I have a <code>Square</code> class that has a BOOL <code>nuked</code>. It also has an NSArray <code>adjacentSquares</code> containing other Squares.</p>
  
  <p>I want to check whether a square, or one of its 'connected' squares, is nuked or not. </p>
  
  <p>The array <code>processedSquares</code> is to keep track of the squares I have checked to prevent infinite recursion.</p>
  
  <p>When I run this, it's doing a lot of calls of this block (as expected). But at some point, it crashes at the last line with a bad access exception.</p>
  
  <p>I also get this in the console:</p>
  
  <blockquote>
    <p>Cannot access memory at address 0x1<br>
    Cannot access memory at address 0x1<br>
    Cannot access memory at address 0x1<br>
    Cannot access memory at address 0x1<br>
    warning: Cancelling call - objc code on the current thread's stack makes this unsafe.</p>
  </blockquote>
  
  <p>I'm not that familiar with blocks and recursion. Any ideas?</p>
  
  <hr>
  
  <h1>Edit 1</h1>
  
  <p>As requested, the backtrace:</p>
  
  <pre><code>#0  0x00000001 in ??
  #1  0x000115fb in -[Square connectedToNukedSquare] at   Square.m:105
  #2  0x00010059 in __-[Bot makeMove]_block_invoke_1 at Bot.m:94
  #3  0x91f3f024 in _dispatch_call_block_and_release
  #4  0x91f31a8c in _dispatch_queue_drain
  #5  0x91f314e8 in _dispatch_queue_invoke
  #6  0x91f312fe in _dispatch_worker_thread2
  #7  0x91f30d81 in _pthread_wqthread
  #8  0x91f30bc6 in start_wqthread
  </code></pre>