My code is crashing with an EXC_BAD_ACCESS error that I don't know how to debug.
This is the code:
NSUInteger lineCount = self.lineBeginnings.count;
NSUInteger lineBeginnings[lineCount];
[self.lineBeginnings getIndexes:lineBeginnings maxCount:lineCount inIndexRange:nil];
It crashes on the last line, with EXC_BAD_ACCESS (code=2, address=0x...)
.
Notice two lines above, it's able to read self.lineBeginnings perfectly, but in the debugger I get:
(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]
Also lineBeginnings doesn't show up properly in the GUI scope browser (all other variables do) and trying to "View Memory of lineBeginnings" gives a completely empty memory view.
The lineBeginnings
variable is stored as a strong @property, it's a mutable index set created in the app delegate's init method and never removed while the app is running. There is a background operation queue that writes to it, but it switches to the main thread for all modifications using dispatch_sync(dispatch_get_main_queue())
.
I'm not sure how to debug this further? It's hard to reproduce, I have to resize the window for up to a minute (which causes the lineBeginnings variable to be re-created on a background queue, a process that takes ~5 minutes when given 180MB of data), in order to make this crash occur.
It looks like a buffer overrun or something to me? How do I track it down?
The source code for the file is here: https://gist.github.com/abhibeckert/7128740 (the crash is on line 254).
In 180 MB there are probably millions of line beginnings? So you are allocating an array of millions of eight byte words on the stack. A thread's stack usually is not that large.
You should allocate the array on the heap, using malloc
.
Your problem is called a stack overflow. Sounds familiar?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With