guillaume guillaume - 4 months ago 36
iOS Question

EXC_BREAKPOINT crash while incrementing an Int

I have a lot of crashes reports in my XCode Organizer.
Here is the trace

Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x00000001000746a8
Triggered by Thread: 0

Breadcrumb Trail: (reverse chronological seconds)
9 GC Framework: startAuthenticationForExistingPrimaryPlayer


Thread 0 name:
Thread 0 Crashed:
0 MyApp 0x00000001000746a8 DataManager.incrementKeywordIndexByTwo() -> () + 284 (DataManager.swift:106)
1 MyApp 0x000000010007995c @objc ViewController.unwindToMainMenu(UIStoryboardSegue) -> () + 68 (ViewController.swift:50)
2 UIKit 0x00000001874ac16c -[UIStoryboardUnwindSegueTemplate _performWithDestinationViewController:sender:] + 216 (UIStoryboardUnwindSegueTemplate.m:471)
3 UIKit 0x00000001874ac05c -[UIStoryboardUnwindSegueTemplate _perform:] + 92 (UIStoryboardUnwindSegueTemplate.m:458)
4 UIKit 0x00000001872fe184 -[UIStoryboardSegueTemplate perform:] + 160 (UIStoryboardSegueTemplate.m:123)
5 UIKit 0x0000000186becbe8 -[UIApplication sendAction:to:from:forEvent:] + 100 (UIApplication.m:4265)
6 UIKit 0x0000000186becb64 -[UIControl sendAction:to:forEvent:] + 80 (UIControl.m:612)
7 UIKit 0x0000000186bd4870 -[UIControl _sendActionsForEvents:withEvent:] + 436 (UIControl.m:694)
8 UIKit 0x0000000186bec454 -[UIControl touchesEnded:withEvent:] + 572 (UIControl.m:446)
9 MyApp 0x0000000100081fe0 specialized GameButton.touchesEnded(Set<UITouch>, withEvent : UIEvent?) -> () + 188 (GameButton.swift:85)
10 MyApp 0x000000010008183c @objc GameButton.touchesEnded(Set<UITouch>, withEvent : UIEvent?) -> () + 128 (GameButton.swift:0)
11 UIKit 0x0000000186bec084 -[UIWindow _sendTouchesForEvent:] + 804 (UIWindow.m:2135)
12 UIKit 0x0000000186be4c20 -[UIWindow sendEvent:] + 784 (UIWindow.m:2257)
13 UIKit 0x0000000186bb504c -[UIApplication sendEvent:] + 248 (UIApplication.m:12647)
14 UIKit 0x0000000186bb3628 _UIApplicationHandleEventQueue + 6568 (UIApplication.m:10454)
15 CoreFoundation 0x0000000181a0d09c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 (CFRunLoop.c:1761)
16 CoreFoundation 0x0000000181a0cb30 __CFRunLoopDoSources0 + 540 (CFRunLoop.c:1807)
17 CoreFoundation 0x0000000181a0a830 __CFRunLoopRun + 724 (CFRunLoop.c:2536)
18 CoreFoundation 0x0000000181934c50 CFRunLoopRunSpecific + 384 (CFRunLoop.c:2814)
19 GraphicsServices 0x000000018321c088 GSEventRunModal + 180 (GSEvent.c:2245)
20 UIKit 0x0000000186c1e088 UIApplicationMain + 204 (UIApplication.m:3772)
21 MyApp 0x0000000100083920 main + 152 (AppDelegate.swift:14)
22 libdyld.dylib 0x00000001814d28b8 start + 4 (start_glue.s:78)


And here is the DataManager code (line 106)

var keywords = [Keyword]()
var keywordIndex = 0
func incrementKeywordIndexByTwo() {
keywordIndex = (keywordIndex + 2) % keywords.count
}


I don't understand what is the cause of the crash, line 106 is just an addition with a modulo.
KeywordIndex and keywords.count are both Int.
And I can't reproduce the crash in my iPad mini 2 or in simulator.

Thanks

Answer

When keywords.count is zero, you are performing a division by zero in effect, which causes an exception.

You need to either check that the keywords array has at least one member or perform a modulo 1 if count is zero.