使用ARC在NS阵列上崩溃的NSFastEnumeration

时间:2012-11-01 20:27:29

标签: objective-c automatic-ref-counting

我正在初始化一个C对象数组并设置第一个元素:

id __strong *_objs =  (id __strong *)calloc(16,sizeof(*_objs));
_objs[0] = @1;
_count++;

然后我使用了以下NSFastEnumeration实现:

- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
                                   objects: (id __unsafe_unretained*)stackbuf
                                     count: (NSUInteger)len
{
    NSUInteger size = _count;
    NSInteger count;
    state->mutationsPtr = (unsigned long *)size;
    count = MIN(len, size - state->state);
    if (count > 0)
    {
        IMP imp = [self methodForSelector: @selector(objectAtIndex:)];
        int p = state->state;
        int i;
        for (i = 0; i < count; i++, p++) {
            stackbuf[i] = (*imp)(self, @selector(objectAtIndex:), p);
        }
        state->state += count;
    }
    else
    {
        count = 0;
    }
    state->itemsPtr = stackbuf;
    return count;
}

不幸的是,当我运行它时,它会与EXC_BAD_ACCESS崩溃:

for (id object in array){  // EXC_BAD_ACCESS
    NSLog(@"%@",object)
}

知道为什么吗?

如果您有CodeRunner,则here是可执行版本。

2 个答案:

答案 0 :(得分:4)

问题是mutationsPtr指向您不允许访问的内存地址1(也不是4字节对齐):

state->mutationsPtr = (unsigned long *)size;

用启动器的有效指针替换它(注意:下面的那个在你的场景中根本没有意义,但至少它修复了EXC_BAD_ACCESS):

state->mutationsPtr = (unsigned long *)&_count;

答案 1 :(得分:1)

@Jano 如果您想摆脱编译器警告,您将获得最新版本的Xcode(4.6)

   - (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
                               objects: (id __unsafe_unretained*)stackbuf
                                 count: (NSUInteger)len

因为它与对象的原始原型不匹配:.. stackbuf

    - (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState*)state
                               objects: (__autoreleasing id *)stackbuf
                                 count: (NSUInteger)len

请参阅Todd Lehmans在Automatic Reference Counting: Error with fast enumeration

中的回答