Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on Save #1

Closed
jonallured opened this issue Nov 11, 2014 · 12 comments
Closed

Crash on Save #1

jonallured opened this issue Nov 11, 2014 · 12 comments

Comments

@jonallured
Copy link
Contributor

I cloned down the repo, built the app and then pressed save - crashed and burned with:

Thread 1: EXC_BAD_ACCESS (code = 1, address=0x1)

I doubt that's very helpful, but I'm not sure what else to do to troubleshoot this. I did stop the app and run it again, then I got another error:

Keychain request failed. Code: -25299. Message: The item already exists.

In both cases, we're stopping on this line:

if let data: NSData = result?.takeRetainedValue() as? NSData {

That's in Locksmith.swift line 40.

@matthewpalmer
Copy link
Owner

Hmm. I haven't been able to reproduce this on my end with a fresh clone. It's strange that that line would cause a crash.

Could you please try replacing that section of code (up until the return) with

if result != nil {
  if let data: NSData = result?.takeRetainedValue() as? NSData {
    // Convert the retrieved data to a dictionary
    resultsDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? NSDictionary
  }
}

and let me know if the error still happens.

Also, what version of Xcode and iOS is this running on?

Thanks so much for your help :)

@jonallured
Copy link
Contributor Author

Hey @matthewpalmer I added that nil check but it didn't do anything. The result must not be nil and execution stops on the next line like before. I also added some logging for the statusCode and error set on lines 36 and 37 - they give me this:

statusCode => -25299
error => Optional(Error Domain=com.locksmith.error Code=-25299 "The operation couldn’t be completed. (com.locksmith.error error -25299.)" UserInfo=0x7c98b540 {message=The item already exists.})

I'm using Xcode 6.1 and the simulator is on iOS 8.1.

Thanks!!
Jon

@matthewpalmer
Copy link
Owner

Does the app completely crash? (i.e. With EXC_BAD_ACCESS as you described before)

The "the item already exists" error means that something has been saved to the keychain using that key before. If you press "load" (in the demo app) it should print out the value of what was saved to the keychain in the log of Xcode. Is there any output when you press 'load', or does it just crash?

Cheers

On 13 Nov 2014, at 1:19 am, Jon Allured notifications@github.com wrote:

Hey @matthewpalmer I added that nil check but it didn't do anything. The result must not be nil and execution stops on the next line like before. I also added some logging for the statusCode and error set on lines 36 and 37 - they give me this:

statusCode => -25299
error => Optional(Error Domain=com.locksmith.error Code=-25299 "The operation couldn’t be completed. (com.locksmith.error error -25299.)" UserInfo=0x7c98b540 {message=The item already exists.})

I'm using Xcode 6.1 and the simulator is on iOS 8.

Thanks!!
Jon


Reply to this email directly or view it on GitHub.

@ullstrm
Copy link

ullstrm commented Nov 13, 2014

I got the exact same error. Have tried to add the nil-check aswell. It's crashing on the same line. Both on simulator and device. (iOS8.1)

@matthewpalmer
Copy link
Owner

Just pushed an update that might have fixed this. Is the crash still happening with da755c7 (i.e. the latest version)?

Thanks so much for your help :)

@jonallured
Copy link
Contributor Author

Hey @matthewpalmer, I pulled down the latest and greatest and then tried again. To be sure nothing was carrying over, I reset my simulator. Then I built and ran the app, pressed "save" and got this error:

fatal error: unexpectedly found nil while unwrapping an Optional value

When I stopped, ran the app again and hit "save" a second time, I got this error:

Keychain request failed. Code: -25299. Message: The item already exists.
fatal error: unexpectedly found nil while unwrapping an Optional value

I stopped the app, ran it once again and hit "load" and got this:

Dictionary: {
    "some key" = "some value";
}

So, that worked! Then I tried to press "delete" and that failed with:

fatal error: unexpectedly found nil while unwrapping an Optional value

I have not upgraded to Xcode 6.1.1, just FYI. I hope that helps and I appreciate your help on this!! :)
Jon

@matthewpalmer
Copy link
Owner

I can't reproduce this, even with a fresh clone and a reset simulator. Could you possibly please track down which line or function it's trying to unwrap the optional in/the app crashes?
Thanks for the detailed error reports—it makes it so much easier on my end :)

@jonallured
Copy link
Contributor Author

Thanks for trying to help me with this - I've actually been having a lot of trouble with the keychain, which is whey I was interested in this project. I keep writing code that I think should work and then I'll get into this weird situation. I'll ask the keychain for a value and it'll say that it doesn't exist. Then I'll try to write it and it'll say that it can't because it's a duplicate. WTF??

To answer your question, the line that's blowing up is if let rawValue = result in performRequest. When I tap "load", I see the key in my console, but when I hit "save" or "delete" that's when execution stops and it doesn't work.

So, in summary, I hate working with the keychain and its unexpected behavior makes me not want to use it.

/rant

matthewpalmer added a commit that referenced this issue Nov 22, 2014
@matthewpalmer
Copy link
Owner

That sucks that you've been having so much trouble. I totally agree, the keychain is tough to deal with.

And it crashes with

fatal error: unexpectedly found nil while unwrapping an Optional value

on that if let rawValue = result line? That's super weird.
I'm thinking it might be because result is defined as var result: Unmanaged<AnyObject>?, so maybe something crazy's going on with the reference to the variable. That's bizarre though because you'd think the if let would account for an invalid optional.

I've just pushed an update that might solve the issue, or at least give us some more information. Could you please run through the save, load, and delete cycle then paste the output here. Thanks so much :)

@fatihpense
Copy link

I was having this exact issue before the update. Issue may be resolved now. Source: It works on my machine :)

@jonallured
Copy link
Contributor Author

Hey @matthewpalmer, that update definitely helped - I'm only seeing failures when I expect them now!!!!

While testing this, I found it helpful to save timestamps instead of just a string, so I opened #3 with that if you're interested. Anyway, here's the results of playing around after a simulator reset...

Pressing Save:

Performing request: service: Locksmith, key: myKey, type: 0, userAccount: LocksmithUser
resultPointer Optional(Swift.Unmanaged)

Pressing Load:

Performing request: service: Locksmith, key: myKey, type: 1, userAccount: LocksmithUser
resultPointer Optional(Swift.Unmanaged)
Dictionary: {
    "some key" = "2014-11-24 19:42:14 +0000";
}

Pressing Delete:

Performing request: service: Locksmith, key: myKey, type: 3, userAccount: LocksmithUser
resultPointer Optional(Swift.Unmanaged)

So, all three worked great - then I wanted to see what tapping multiple times would do.

Multiple Saves:

Performing request: service: Locksmith, key: myKey, type: 0, userAccount: LocksmithUser
resultPointer Optional(Swift.Unmanaged)
Performing request: service: Locksmith, key: myKey, type: 0, userAccount: LocksmithUser
Keychain request failed. Code: -25299. Message: The item already exists.
resultPointer Optional(Swift.Unmanaged)

Multiple Deletes:

Performing request: service: Locksmith, key: myKey, type: 3, userAccount: LocksmithUser
resultPointer Optional(Swift.Unmanaged)
Performing request: service: Locksmith, key: myKey, type: 3, userAccount: LocksmithUser
Keychain request failed. Code: -25300. Message: The item cannot be found.
resultPointer Optional(Swift.Unmanaged)

My only remaining crash is when I tap 'Delete', then 'Load':

Performing request: service: Locksmith, key: myKey, type: 1, userAccount: LocksmithUser
Keychain request failed. Code: -25300. Message: The item cannot be found.
resultPointer Optional(Swift.Unmanaged)

Fails in Locksmith.swift, line 46:

if let data = result?.takeUnretainedValue() as? NSData {

That same thread error I originally reported. I don't get it - I've tried unwrapping that damn result object and it just doesn't seem to behave the way I expect. So, if it's going to be a lying liar, maybe something like this instead;

if type == .Read && status == errSecSuccess {
  if let data = result!.takeUnretainedValue() as? NSData {
    resultsDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? NSDictionary
  }
}

Here we just rely on the status being successful, which seems safer. And this worked for me - I can blow away the token and try loading without a crash. It won't find anything, but it shouldn't anyway.

So, I'm going to close this one and thank you for hanging in there and helping me - I really appreciate it!! :)

Jon

@matthewpalmer
Copy link
Owner

I'll implement this fix now. Thanks for all your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants