【发布时间】:2018-02-26 17:33:55
【问题描述】:
问题: 我不断收到 EXC_BAD_ACCESS。在我打开 NSZombieEnabled 后,我看到了这个 [FeatureCommentListViewController respondsToSelector:]: message sent to deallocated instance 0x7c1dc30
在我将项目改成ARC之前没有这个错误,但是我改成ARC之后就出现了这个错误。
-
我在 Block 中声明了一个 ViewController 并将其推送到导航控制器中。这个原因会导致它的寿命更短吗?
UIBlockButton 来自this post
UIBlockButton *lbGood3 = [[UIBlockButton alloc] initWithFrame:CGRectMake(0, 0, First_Button_Width, [self getGoodRow2Height:productDetail]) ]; [lbGood3 handleControlEvent:UIControlEventTouchUpInside withBlock:^ { NSLog(@"%@", Label.text); ProductDetail *productDetail = [productDetailDict objectForKey:@"product"]; NSString *dp_id = [NSString stringWithFormat:@"%@-%@",productDetail.url_crc,productDetail.site_id]; FeatureCommentListViewController *cmtListController = [[FeatureCommentListViewController alloc] initWithNibName:@"FeatureCommentListViewController" bundle:nil]; cmtListController.title = Label.text; cmtListController.isReviewed=isReviewed; cmtListController.productDetail=productDetail; cmtListController.dp_id=dp_id; cmtListController.feature_name = @"&feature_good_id=2"; [self.navigationController pushViewController:cmtListController animated:YES]; }];
我应该将控制器声明为这个 viewController 的成员还是直接声明出来?
【问题讨论】:
-
您从哪里获得 EXC_BAD_ACCESS?
-
我想知道为什么将视图控制器推送到块中而不是主线程中?对触摸动作做出快速响应不是很重要吗?附言。如果您将 vc 声明为块外,则可以在其前面添加 __block,如 Hermann Klecker 所述。
-
你有堆栈跟踪吗? EXC_BAD_ACCESS 是在块内发生还是在控制器被推后发生?当你推送它时,你的导航控制器应该保留
cmtListController,并且ARC应该在最后一次访问cmtListController之后在块的末尾添加一个[cmtListController release]。 -
@ChristopherPickslay 实际上没有。因为 EXC_BAC_ACCESS 发生在网络请求的回调函数中。但我可以得到释放对象的类(FeatureCommentListViewController)。我通过在 viewDidLoad 函数中分配 FeatureCommentListViewController 并在块中使用它来解决这个问题。