EXC_BAD_ACCESS when using recursive block

后端 未结 3 678
面向向阳花
面向向阳花 2020-12-30 13:14

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:

BOOL (^Bl         


        
3条回答
  •  清酒与你
    2020-12-30 14:19

    You're liking doing something wrong with the setup -- your Square objects are probably messed up somehow. Here's a complete example that works fine for me, maybe it can help you find your mistake:

    #include 
    #include 
    
    @interface Square : NSObject
    {
      BOOL nuked;
      NSArray *adjacentSquares;
    }
    
    @property(nonatomic) BOOL nuked;
    @property(nonatomic, retain) NSArray *adjacentSquares;
    @end
    
    @implementation Square
    
    @synthesize nuked;
    @synthesize adjacentSquares;
    
    @end;
    
    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;
    };
    
    int main(int argc, char **argv)
    {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
      Square *s1, *s2;
      s1 = [[Square alloc] init];
      s2 = [[Square alloc] init];
      s1.adjacentSquares = [NSArray arrayWithObjects:s2, nil];
      s2.adjacentSquares = [NSArray arrayWithObjects:s1, nil];
    
      __block NSMutableArray *processedSquares = [NSMutableArray array];
      BOOL foundNukedSquare = Block(s1, processedSquares);
      printf("%d\n", foundNukedSquare);
    
      [s1 release];
      [s2 release];
    
      [pool release];
    
      return 0;
    }
    

提交回复
热议问题