使用递归块时EXC_BAD_ACCESS

时间:2011-01-30 18:03:12

标签: iphone objective-c cocoa-touch recursion objective-c-blocks

我正在尝试使用块创建递归。它工作了一段时间,但最终它崩溃并给我一个糟糕的访问异常。这是我的代码:

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);

说明:我有一个Square类,其中包含BOOL nuked。它还有一个包含其他Squares的NSArray adjacentSquares

我想检查一个正方形或其中一个“连接”方块是否被破坏。

数组processedSquares用于跟踪我检查过的方块,以防止无限递归。

当我运行它时,它正在执行此块的大量调用(如预期的那样)。但是在某些时候,它在最后一行崩溃并出现访问异常。

我也在控制台中得到了这个:

  

无法访问地址0x1的内存
  无法访问地址0x1的内存   无法访问地址0x1的内存   无法访问地址0x1的内存   警告:取消当前线程堆栈上的调用 - objc代码会使其不安全。

我不熟悉块和递归。有什么想法吗?


编辑1

根据要求,回溯:

#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

3 个答案:

答案 0 :(得分:14)

__block上需要Block,请将声明更改为:

__block BOOL (^Block)(Square *square, NSMutableArray *processedSquares);
Block = ^(Square *square, NSMutableArray *processedSquares) {

当一个块中引用变量(Block)时,它的当前被复制到块中。在您的代码Block中尚未给出值,因为您正在分配中构建块...

__block前缀通过引用传递变量 - 当块使其递归调用Block具有值时,对它的引用用于获取该值值,递归调用就可以了。

我不知道为什么它在没有__block的情况下为你工作 - 对我来说失败了。但是使用修饰符,我可以递归到至少10,000的深度 - 所以堆栈空间不是问题!

答案 1 :(得分:1)

您喜欢设置错误 - 您的Square对象可能会以某种方式搞砸了。这是一个适合我的完整示例,也许它可以帮助您找到错误:

#include <stdio.h>
#include <Foundation/Foundation.h>

@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;
}

答案 2 :(得分:0)

在遍历数组时,您似乎在向数组添加squares。我说的是这句话:

[processedSquares addObject:square];

可能与它有关吗?您在遍历时添加了一个对象。我很惊讶这一点都有用。

相关问题