[HelpViewController respondsToSelector] message sent to deallocated instance

Yesterday I have met one annoying problem: after I have set up IAP (Inner App Purchase ), 

I try to do the test, when I purchase the first time, it is no problem at all, but if I back to the main window from the subview, and then enter subview to purchase again, the App crashed. 

      The following is the screen shot: 

 

     I have tried many times to locate the problem, and I have found it always after I have back to superview and enter subview, the problem always stop at: [[SKPaymentQueue defaultQueue] addPayment:payment];

     Then I searched it in google, and have found the same problem in stackoverflow.com

http://stackoverflow.com/questions/11170614/viewcontroller-respondstoselector-message-sent-to-deallocated-instance-crash

this article said the crash reason is "The crash occurs when that controller was dealloc'ed but was still the delegate for the view controller.”

according to this article, i have added some codes below: 

-(void) viewWillDisappear:(BOOL) animated

{

    [super viewWillDisappear:animated];

    if ([self isMovingFromParentViewController])

    {

        if (productsRequest.delegate == self)

        {

            productsRequest.delegate = nil;

        }

    }

}

    but the problem still there!

 

     Then I thought it should related with dealloc  , because  "message sent to deallocated instance”. so I search method  -(void)dealloc , the method is already there , came from init view controller, there is some description of  “we need to manually stop observing any NSNotificationCenter notifications. Otherwise we could get "zombie" crashes”. And then I have found I add one observer in “view did load", but I have not removed it after view controller deallocated. 

 

-(void)viewDidLoad 

    { 

  [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; object:nil];

    }

 

     After I have removed Transaction Observer in dealloc , then the problem solved.

 

 -(void)dealloc

 {

        [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];

 }

posted @ 2015-03-25 21:46  Jacky_0602  阅读(843)  评论(0编辑  收藏  举报