EXC_BAD_ACCESS时使用递归块 [英] EXC_BAD_ACCESS when using recursive block

查看:150
本文介绍了EXC_BAD_ACCESS时使用递归块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 (^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 。它还有一个包含其他正方形的NSArray adjacentSquares

Explanation: I have a Square class that has a BOOL nuked. It also has an NSArray adjacentSquares containing other Squares.

我想检查一个正方形, 'connected'正方形,是否nuked。

I want to check whether a square, or one of its 'connected' squares, is nuked or not.

数组 processedSquares 是跟踪我检查过的方格以防止无限递归。

The array processedSquares is to keep track of the squares I have checked to prevent infinite recursion.

当我运行这个,它做了很多的这个块的调用(如预期)。但是在某些时候,它在最后一行崩溃,访问异常不好。

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.

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

I also get this in the console:


无法访问地址0x1处的内存

无法访问地址0x1处的内存

无法访问地址0x1处的内存

无法访问地址0x1处的内存

警告:在当前线程的堆栈中取消调用 - objc代码会使此操作不安全。

Cannot access memory at address 0x1
Cannot access memory at address 0x1
Cannot access memory at address 0x1
Cannot access memory at address 0x1
warning: Cancelling call - objc code on the current thread's stack makes this unsafe.

我不熟悉块和递归。任何想法?

I'm not that familiar with blocks and recursion. Any ideas?

backtrace:

As requested, the backtrace:

#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


推荐答案

您需要一个 __ block 阻止,将声明更改为:

You need a __block on Block, change the declaration to:

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

Block )在块中被引用,则其当前的被复制到块中。在您的代码阻止还没有赋予一个值,因为你正在构造块中的赋值...

When a variable (Block) is referenced within a block then its current value is copied into the block. In your code Block hasn't yet been given a value, as you are constructing the block in the assignment...

__ block 前缀通过引用传递变量 - 在您的块进行递归调用时 code>有一个值,对它的引用用于获取该值,并且递归调用是OK。

The __block prefix passes the variable by reference - by the time your block makes its recursive call Block has a value, the reference to it is used to obtain that value, and the recursive call is OK.

我不知道为什么它的工作对你没有 __块 - 对我失败的直截了当。使用修饰符,我可以递归到至少10,000的深度 - 所以堆栈空间不是问题!

I don't know why its worked at all for you without the __block - failed straightaway for me. With the modifier however I can recurse to at least a depth of 10,000 - so stack space isn't a problem!

这篇关于EXC_BAD_ACCESS时使用递归块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆