Vishal
Vishal

Reputation: 467

EXC_BAD_ACCESS (code=1, address=0x0)

I have a piece of code which runs correctly in real device but is giving EXC_BAD_ACCESS (code=1, address=0x0) error in simulator (iphone 6) : enter image description here

The bt output is:

(lldb) bt
* thread #1: tid = 0xe40f6, 0x00000001052ab5e9 CoreFoundation`CFArrayGetCount + 25, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00000001052ab5e9 CoreFoundation`CFArrayGetCount + 25
  * frame #1: 0x0000000104c6a5af MyApp`static MyApp.SSID.fetchSSIDInfo (self=MyApp.SSID)() -> Swift.String + 95 at WifiGetter.swift:17
    frame #2: 0x0000000104c4994a MyApp`MyApp.TableViewController.sendWifi (self=0x00007ff05a64da80)() -> () + 26 at TableViewController.swift:355
    frame #3: 0x0000000104c49316 MyApp`MyApp.TableViewController.fetchData (self=0x00007ff05a64da80)() -> () + 38 at TableViewController.swift:308
    frame #4: 0x0000000104c4567f MyApp`MyApp.TableViewController.viewDidLoad (self=0x00007ff05a64da80)() -> () + 3215 at TableViewController.swift:66
    frame #5: 0x0000000104c45ea2 MyApp`@objc MyApp.TableViewController.viewDidLoad (MyApp.TableViewController)() -> () + 34 at TableViewController.swift:0
    frame #6: 0x00000001063c9931 UIKit`-[UIViewController loadViewIfRequired] + 1344
    frame #7: 0x000000010640cc26 UIKit`-[UINavigationController _layoutViewController:] + 54
    frame #8: 0x000000010640d4dd UIKit`-[UINavigationController _updateScrollViewFromViewController:toViewController:] + 433
    frame #9: 0x000000010640d633 UIKit`-[UINavigationController _startTransition:fromViewController:toViewController:] + 116
    frame #10: 0x000000010640e879 UIKit`-[UINavigationController _startDeferredTransitionIfNeeded:] + 890
    frame #11: 0x000000010640f67d UIKit`-[UINavigationController __viewWillLayoutSubviews] + 57
    frame #12: 0x00000001065a763d UIKit`-[UILayoutContainerView layoutSubviews] + 248
    frame #13: 0x00000001062ef11c UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
    frame #14: 0x000000010c35836a QuartzCore`-[CALayer layoutSublayers] + 146
    frame #15: 0x000000010c34cbd0 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 366
    frame #16: 0x000000010c34ca4e QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 24
    frame #17: 0x000000010c3411d5 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 277
    frame #18: 0x000000010c36e9f0 QuartzCore`CA::Transaction::commit() + 508
    frame #19: 0x000000010c36f154 QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 92
    frame #20: 0x00000001053079d7 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    frame #21: 0x0000000105307947 CoreFoundation`__CFRunLoopDoObservers + 391
    frame #22: 0x00000001052fcebc CoreFoundation`CFRunLoopRunSpecific + 524
    frame #23: 0x000000010623998d UIKit`-[UIApplication _run] + 402
    frame #24: 0x000000010623e676 UIKit`UIApplicationMain + 171
    frame #25: 0x0000000104c20efd MyApp`main + 109 at AppDelegate.swift:20
    frame #26: 0x0000000108ea592d libdyld.dylib`start + 1

Upvotes: 3

Views: 4876

Answers (2)

jtbandes
jtbandes

Reputation: 118671

interfaces is nil. You must handle this case. I recommend avoiding !. Making the type non-optional here will allow the if let binding to work correctly.

Upvotes: 3

Doan Cuong
Doan Cuong

Reputation: 2624

You should change your if let clause to if let interfaces = CNCopySupportedInterfaces() as? CFArray.

Because you use if let interfaces: CFArray! with ! mean force unwrap, therefore, when CNCopySupportedInterfaces() return nil or value which is not convertible to CFArray, force unwrap will cause crash

Upvotes: 0

Related Questions