[GH-ISSUE #276] IOS10 Crash of '[unowned self]' #165

Closed
opened 2026-03-03 16:46:16 +03:00 by kerem · 2 comments
Owner

Originally created by @zenyuhao on GitHub (Sep 21, 2016).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/276

== Describe here the steps to reproduce, with error message if any, and your code if relevant ==

  • **OS targeted (with version):10.0 (iphone6)
  • **OAuth provider:twitter
  • **OAuthSwift version:0.6.0

Hi, I found a bug (maybe) in IOS10

look at this line, the code [unowned self] may cause a crash in ios10 , it will not happen in ios9

    // 1. Request token
    func postOAuthRequestTokenWithCallbackURL(callbackURL: NSURL, success: TokenSuccessHandler, failure: FailureHandler?) {
        var parameters =  Dictionary<String, AnyObject>()
        if let callbackURLString: String = callbackURL.absoluteString {
            parameters["oauth_callback"] = callbackURLString
        }
        self.client.post(self.request_token_url, parameters: parameters, success: {
            [unowned self] data, response in  // <-- here
            let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) as String!
            let parameters = responseString.parametersFromQueryString()
            if let oauthToken=parameters["oauth_token"] {
                self.client.credential.oauth_token = oauthToken.safeStringByRemovingPercentEncoding
            }
            if let oauthTokenSecret=parameters["oauth_token_secret"] {
                self.client.credential.oauth_token_secret = oauthTokenSecret.safeStringByRemovingPercentEncoding
            }
            success(credential: self.client.credential, response: response, parameters: parameters)
        }, failure: failure)
    }

and i debug it, the self may be already released
It generally happens at app launch
and i try to delete the "unowned self",it works well, no crash anymore。
dose any thing wrong with it?

forgive my poor english

Originally created by @zenyuhao on GitHub (Sep 21, 2016). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/276 == Describe here the steps to reproduce, with error message if any, and your code if relevant == - **OS targeted (with version):10.0 (iphone6) - **OAuth provider:twitter - **OAuthSwift version:0.6.0 Hi, I found a bug (maybe) in IOS10 look at this line, the code `[unowned self]` may cause a crash in ios10 , it will not happen in ios9 ``` // 1. Request token func postOAuthRequestTokenWithCallbackURL(callbackURL: NSURL, success: TokenSuccessHandler, failure: FailureHandler?) { var parameters = Dictionary<String, AnyObject>() if let callbackURLString: String = callbackURL.absoluteString { parameters["oauth_callback"] = callbackURLString } self.client.post(self.request_token_url, parameters: parameters, success: { [unowned self] data, response in // <-- here let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) as String! let parameters = responseString.parametersFromQueryString() if let oauthToken=parameters["oauth_token"] { self.client.credential.oauth_token = oauthToken.safeStringByRemovingPercentEncoding } if let oauthTokenSecret=parameters["oauth_token_secret"] { self.client.credential.oauth_token_secret = oauthTokenSecret.safeStringByRemovingPercentEncoding } success(credential: self.client.credential, response: response, parameters: parameters) }, failure: failure) } ``` and i debug it, the **self** may be already released It generally happens at app launch and i try to delete the "unowned self",it works well, no crash anymore。 dose any thing wrong with it? forgive my poor english
kerem 2026-03-03 16:46:16 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@phimage commented on GitHub (Sep 21, 2016):

"unowned self" must be replaced by "weak self" here
with
"guard let this = self else { OAuthSwift.retainError(failure); return }"

keep a reference to your oauthSwift object and it will be not released

<!-- gh-comment-id:248584281 --> @phimage commented on GitHub (Sep 21, 2016): "unowned self" must be replaced by "weak self" here with "guard let this = self else { OAuthSwift.retainError(failure); return }" keep a reference to your oauthSwift object and it will be not released
Author
Owner

@zenyuhao commented on GitHub (Sep 21, 2016):

3ks , got it, not notice that master has been repaired
3ks again

<!-- gh-comment-id:248585016 --> @zenyuhao commented on GitHub (Sep 21, 2016): 3ks , got it, not notice that master has been repaired 3ks again
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#165
No description provided.