I have searched quite a while for this answer with the following keywords on Google but I got nothing:
+applicationDidEnterBackground +applicationWillEnterForeground +Facebook +dealloc +SSO
Here is my problem statement:
开发者_运维百科If I press home button to suspend the application and click on the app icon to resume it, no matter how many times I tried, the following function will be called in the correct order, with no dealloc function being called:
applicationDidEnterBackground - upon pressing home button
applicationWillEnterForeground - upon pressing app icon
Then, if within an App I start a call to Facebook's SSO:
[facebook authorize:_permissions delegate:self];
Strange things happen:
applicationDidEnterBackground - upon Facebook SSO
Press home button to exit Safari's Facebook SSO page
applicationWillEnterForeground - upon pressing app icon
delegate's dealloc is called soon after applicationWillEnterForeground ends
(very first function called after some assembly codes:
mov 0x56314c(%ebx),%esi
xor %eax,%eax
test %al,%al
mov 0x8(%ebp),%ecx
=> dealloc
I don't really know what is going on here, and I have tried to search for the answers on Google for quite a while now, can someone help to explain why this is happening?
I am using Facebook version "kSDKVersion = @"2";".
Additional Information:
After taking a look into the problem, I think the problem lies deeper within how my view controller interacts with navigation controller when the app enters foreground again:
My Facebook SSO is invoked within RegistrationViewController's XIB where user clicks a button to enter SSO session, to display the RegistrationViewController, I used the following codes:
[_delegate.navigationController popViewControllerAnimated:NO];
_delegate.regViewController = [[RegistrationViewController alloc] initWithNibName:nil bundle:nil];
[_delegate.navigationController pushViewController:_delegate.regViewController animated:NO];
[_delegate.regViewController release];
After user clicks the button inside RegistrationViewController, Safari is opened. Pressing home button to exit Safari and clicking the app icon will first call:
applicationWillEnterForeground
Then it calls the dealloc function of RegistrationViewController, which is the part I don't understand, since RegistrationViewController should have been retained by Navigation Controller isn't it? Why is it executing the dealloc?
Also, within my RegistrationViewController dealloc routine, since I have declared a _delegate pointer within my RegistrationViewController using sharedApplication, I think it is only logical to dealloc it when RegistrationViewController no longer is needed, but why would [_delegate release] lead to a permanent demise of my delegate? Shouldn't delegate be retained by someone else all the time?
The solution to my problem is that the fact I am releasing what I shouldn't be releasing!
The following statement:
_delegate = (PHPConnectDemoAppDelegate *)[[UIApplication sharedApplication] delegate];
will not increment the retain count to the app delegate by 1.
Therefore it is not necessary within the same View Controller's dealloc function to perform:
[_delegate release];
or else the retain count to your app delegate drops down to 0 hence leading to a permanent demise of your app delegate.
Somehow I think the assembly code I pasted is performing assessment on the retain count to decide whether to dealloc the app delegate or not, correct me if I am wrong.
精彩评论