您的位置:首页 > 其它

Message sent to deallocated instance 问题解决方法

2015-03-25 21:38 639 查看
昨天我遇到一个很难缠的问题: 我在应用中创建了IAP (Inner App Purchase ), 在测试过程中发现,如果我只内购一次,没有问题,但是如果我退出内购所在的About View Controller,然后再进入内购界面购买,应用就会崩溃。截屏如下:



为了定位问题,我测试了很多次,发现这个问题总是出现在我退回主界面再回到内购界面之后,而且程序中止于:[[SKPaymentQueue defaultQueue] addPayment:payment];

然后我就在google中搜索,在stackoverflow.com中找到一篇类似的案例,
http://stackoverflow.com/questions/11170614/viewcontroller-respondstoselector-message-sent-to-deallocated-instance-crash
这个案例说程序崩溃的原因是controller 已经 deallocated , 但是某个对象的delegate仍然指向这个controller。依据这个案例,我添加了以下代码:

-(void) viewWillDisappear:(BOOL) animated

{

[super viewWillDisappear:animated];

if ([self isMovingFromParentViewController])

{ if (productsRequest.delegate == self)

{ productsRequest.delegate = nil;}

}

}

但是问题依然存在!

然后我想这个问题应该与dealloc相关,因为错误提示"message sent to deallocated instance”, 我在程序中找到method -(void)dealloc,是在程序初始创建时,已经存在了。

里面有相关的描述:“we need to manually stop observing any NSNotificationCenter notifications. Otherwise we could get "zombie" crashes”.

同时我发现我已经在 -(void)viewDidLoad添加了observer,但是我在view controller deallocated之后,并没有去除。

-(void)viewDidLoad

{

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

}

我在 -(void)dealloc去除 observer, 最终问题得到解决.

-(void)dealloc

{

[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐