奇怪的行为:消息发送到解除分配的实例

时间:2012-08-11 06:06:57

标签: iphone objective-c ios

在我看来,我有一个按钮,它与这个方法挂钩:

-(IBAction)showStore:(id)sender
{
    storeSinglePlayer *ssp = [[storeSinglePlayer alloc] init];
    ssp.imageH = self.imageURLH;
    ssp.imageV = self.imageURLV;
    CATransition* transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush; 
    transition.subtype = kCATransitionFromTop;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController pushViewController:ssp animated:NO];
 }

当我点击此按钮时,它会向我显示“存储单人游戏”'查看控制器。此外,该商店的单人游戏'视图控制器有一个后退按钮,它挂钩到以下方法:

-(IBAction)didPressBack:(id)sender
{
    CATransition* transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush; 
    transition.subtype = kCATransitionFromBottom;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController popViewControllerAnimated:NO];

}

单击后退按钮也会让我回到之前的视图。

但现在这是一个奇怪的问题。如果我重复这10次,即点击按钮显示' storeSinglePlayer'然后点击' storeSinglePlayer'上的后退按钮,我收到错误:

[storeSinglePlayer respondsToSelector:]: message sent to deallocated instance 0xe5f6160

并且,这不仅仅是在我的多次尝试中,它只是随机崩溃。它会工作一段时间,有时会赢了。

我启用了ARC。

仅仅通过这个简单的代码就无法解决问题。阅读很多关于堆栈溢出的问题,但没有解决我的问题。

编辑:堆栈跟踪

2012-08-11 11:55:56.353 Magic Buzz[2088:707] (
    0   Magic Buzz                          0x000c7e19 -[gameOverScreen showStore:] + 700
    1   CoreFoundation                      0x3769c3fd -[NSObject performSelector:withObject:withObject:] + 52
    2   UIKit                               0x30891e07 -[UIApplication sendAction:to:from:forEvent:] + 62
    3   UIKit                               0x30891dc3 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 30
    4   UIKit                               0x30891da1 -[UIControl sendAction:to:forEvent:] + 44
    5   UIKit                               0x30891b11 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 492
    6   UIKit                               0x30892449 -[UIControl touchesEnded:withEvent:] + 476
    7   UIKit                               0x3089092b -[UIWindow _sendTouchesForEvent:] + 318
    8   UIKit                               0x30890319 -[UIWindow sendEvent:] + 380
    9   UIKit                               0x30876695 -[UIApplication sendEvent:] + 356
    10  UIKit                               0x30875f3b _UIApplicationHandleEvent + 5826
    11  GraphicsServices                    0x3789222b PurpleEventCallback + 882
    12  CoreFoundation                      0x37716523 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 38
    13  CoreFoundation                      0x377164c5 __CFRunLoopDoSource1 + 140
    14  CoreFoundation                      0x37715313 __CFRunLoopRun + 1370
    15  CoreFoundation                      0x376984a5 CFRunLoopRunSpecific + 300
    16  CoreFoundation                      0x3769836d CFRunLoopRunInMode + 104
    17  GraphicsServices                    0x37891439 GSEventRunModal + 136
    18  UIKit                               0x308a4cd5 UIApplicationMain + 1080
    19  Magic Buzz                          0x000b094d main + 96
    20  Magic Buzz                          0x000b08e8 start + 40
)
2012-08-11 11:55:56.554 Magic Buzz[2088:707] *** -[storeSinglePlayer respondsToSelector:]: message sent to deallocated instance 0xf6cde30

1 个答案:

答案 0 :(得分:2)

一些异步方法在解除分配后调用storeSinglePlayer对象,尝试在ViewDidUnload / ViewWillDisappear中删除所有委托为nil。如果您将某些查询发送到服务器而不等待它,则按“返回”按钮,然后它也会崩溃,因为在完成请求后它将调用storeSinglePlayer对象。希望这有帮助