Initializing a variable causes crash

Hi, I've got a very strange issue that I can't resolve: in one piece of code only (the one I'm working on, which worked fine previously), any varible that's initialized at the top of my InterfaceController.swift (Apple Watch Extension) file throws a "EXC_BAD_ACCESS (Code = 2....)" error. This started with the following line, but even if I take it out, any variable that is initialized at the start throws the same error:

var locManager = CLLocationManager()


As I said, this used to work fine, and I've even tested other apps that I'm working on, and they all work fine.


I've tried deleting the app in the simulator, and cleaning the build folder, but I get the same error--and like I said if I remove that line from my code, any other variable that is initialized throws the same error. Hopefully someone has seen this before and can help!

To be clear: this is with the latest XCode beta, using watchOS and Swift 2.

Interesting. Yeah, that's really weird. Have you checked out the Zombie Objects or Address Sanitizer features? They might be able to help you pinpoint your problem. Both are accessible when you edit your scheme, head to the Run action and click on the Diagnostics tab. After enabling them, run your app. Now when it crashes, you'll get a detailed readout on what went wrong.


Say, did you check to see if anything was wrong with your source file? Take a look in the File Inspector. Maybe also create another Swift file with a function in it that does the same thing as your setup code, and call that function from your original file.


Other than that, I'm just as stumped as you are.

Hi, Bob, Sorry for the delay here. I've been beating my head against this problem. I migrated my entire program to a new project (what a pain) only to have the same thing happen again! Also, I get no results when I use Zombie objects, and address sanitization fails the build (something about it doesn't know where the library is), so I can't even use that. Good suggestions, but no go. I was attempting to migrate my app from one watch screen to three. I assume something to do with that has caused the issue, but I'm clueless as to what it could be. I will have to disassemble the app and put it back together piece by piece to see when the problem occurs. Clearly I'm not getting any kind of adequate data to debug it.

If address sanitation causes the build to fail, maybe that's a clue! You mentioned having a problem with where libraries are… did you make sure you aren't trying to link to any strange frameworks or libraries? Other than that, I'm not sure I can help you.


But don't lose heart. I've had weird, mysterious problems crop up before (like windows in my OS X app completely failing to show up) that were just as mysteriously solved the next day. I still haven't figured out what happened. If you're still having trouble tomorrow, I'd recommend filing a bug report using Apple's Bug Reporter tool: http://bugreport.apple.com

Thanks. I need to look into that whole issue with Libraries. I'm going to take my code apart and just display one watch page at a time. Assuming I can get one to run, perhaps I can figure out where the issue is arising. If not, I'll likely file a bug report. At least Apple tech might help me in that case 😉


Thanks for the good wishes!

Yeah, that's one of my favorite debugging strategies—assume that what I just changed caused the problem and start removing/disabling components until it's isolated.


Oh, and if you do file a bug report, don't expect prompt service. In my experience, you'll wait several weeks for a response. I've got some that I filed last year that are still pending.


Good luck!

Well I've narrowed things down, and it does have to do with turning my watch app into 3 pages. Apparently this line is what makes it crash, even though the CLLocation line is what's being highlighted.

let settingsVC = SettingsScreenInterfaceController()

That would be my settings page, where I've moved some variables I need to check back in my main IC. I normally use the AppDelegate in the iOS to store variables I'll be using across VCs, but that's not available in the Apple Watch extension. How is one supposed to share variables between ICs in Apple Watch? I clearly must be doing something wrong!

You're writing a watchOS 2 app, right? Those are built more like traditional apps, except that a few components are differently set up. I haven't worked with Apple Watch, but to the best of my knowledge, the WKExtension class is basically parallel to UI/NSApplication. Like in UIApplication, there is a WKExtensionDelegate protocol that works essentially the same way. So if you create a delegate for your WKExtension, you should be able to access all of the features that an app delegate normally has (like the ability to modify and persist global state).


WKExtension class reference:

https://developer.apple.com/library/prerelease/watchos/documentation/WatchKit/Reference/WKExtension_class/index.html#//apple_ref/doc/uid/TP40015522


WKExtensionDelegate protocol reference:

https://developer.apple.com/library/prerelease/watchos/documentation/WatchKit/Reference/WKExtensionDelegate_protocol/index.html#//apple_ref/swift/intf/c:objc(pl)WKExtensionDelegate

Initializing a variable causes crash
 
 
Q