Objective-C奇怪的EXC_BAD_ACCESS

时间:2013-10-23 23:40:39

标签: objective-c cocoa crash

我的代码因EXC_BAD_ACCESS错误而崩溃,我不知道如何调试。

这是代码:

NSUInteger lineCount = self.lineBeginnings.count;
NSUInteger lineBeginnings[lineCount];
[self.lineBeginnings getIndexes:lineBeginnings maxCount:lineCount inIndexRange:nil];

它在最后一行崩溃,EXC_BAD_ACCESS (code=2, address=0x...)

注意上面两行,它能够完美地读取self.lineBeginnings,但在调试器中我得到:

(lldb) p [self lineBeginnings]
error: Trying to put the stack in unreadable memory at: 0x7fff5d15e310.
(lldb) p _lineBeginnings
(NSMutableIndexSet *) $1 = 0x0000610000059b90
(lldb) po _lineBeginnings
[no Objective-C description available]

另外lineBeginnings在GUI范围浏览器中没有正确显示(所有其他变量都有)并且尝试“查看lineBeginnings的内存”会给出一个完全空的内存视图。

lineBeginnings变量存储为强大的@property,它是在app delegate的init方法中创建的可变索引集,在应用程序运行时从不删除。有一个后台操作队列写入它,但它使用dispatch_sync(dispatch_get_main_queue())切换到主线程进行所有修改。

我不确定如何进一步调试?它很难重现,我必须调整窗口大小一分钟(这会导致lineBeginnings变量在后台队列上重新创建,这个过程在给定180MB数据时需要大约5分钟),以便制作发生这种崩溃。

它看起来像是缓冲区溢出或对我而言?如何追踪它?

该文件的源代码位于:https://gist.github.com/abhibeckert/7128740(崩溃位于第254行)。

2 个答案:

答案 0 :(得分:12)

在180 MB中,可能有数百万行开头?因此,您要在堆栈上分配数百万个八字节字的数组。线程的堆栈通常不是那么大。

您应该使用malloc在堆上分配数组。

您的问题称为堆栈溢出。听起来很熟悉?

答案 1 :(得分:0)

我遇到了同样的问题,花了2天后我发现我的吸气器正在呼叫几次因为我在吸气器中使用自我。

 if(! _openSectionIndexes) _openSectionIndexes = [NSMutableArray new];

if( _openSectionIndexes.count != _requests.count)
{
    for (int i =0; i < _requests.count; i++)
    {
        [self.openSectionIndexes addObject: @(NO)];// here was the problem, replaced it with _openSectionIndexes
    }
}

return _openSectionIndexes;