UISearchDisplayController自动释放如何在其他视图控制器中导致崩溃? [英] How can UISearchDisplayController autorelease cause crash in a different view controller?

查看:75
本文介绍了UISearchDisplayController自动释放如何在其他视图控制器中导致崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器A和B.从A导航到视图控制器B,如下所示:

I have two view controllers A and B. From A, I navigate to view controller B as follows:

// in View Controller A 
// navigateToB method

-(void) navigateToB {

BViewController *bViewController = 
[[BViewController alloc] initWithNibName: @"BView" bundle:nil];

bViewController.bProperty1 = SOME_STRING_CONSTANT;
bViewController.title = @"A TITLE OF A VC's CHOOSING"; 
[self.navigationController pushViewController: bViewController animated:YES];
[bViewController release]; //<----- releasing 0x406c1e0

}

在BViewController中,属性bPropery1是通过copy定义的,如下所示(注意,B还包含UITableView和其他属性):

In BViewController, the property bPropery1 is defined with copy as below (note, B also contains UITableView and other properties):

@property (nonatomic, copy) NSString *bProperty1;

在A和B之间来回导航时,一切似乎都可以正常工作.直到我将UISearchDisplayController添加到BViewController中包含的表视图中之前.现在,当我从B导航出来时,回到A,该应用程序崩溃了.

Everything appeared to work fine when navigating back and forth between A and B. That is until I added a UISearchDisplayController to the table view contained in BViewController. Now when I navigate out of B, back to A, the app crashes.

堆栈跟踪显示崩溃时自动释放的搜索显示控制器的外观:

Stack trace shows what looks the search display controller being autoreleased at time of crash:

#0 0x009663a7 in ___forwarding___
#1 0x009426c2 in __forwarding_prep_0___
#2 0x018c8539 in -[UISearchDisplayController _destroyManagedTableView]
#3 0x018c8ea4 in -[UISearchDisplayController dealloc]
#4 0x00285ce5 in NSPopAutoreleasePool

NSZombies显示:

NSZombies shows:

-[BViewController respondsToSelector:]: message sent to deallocated instance 0x406c1e0

malloc的历史记录指向上面A的navigationToB方法中已经发布的bViewController:

And malloc history on this points to the bViewController already released in A's navigateToB method above:

    Call [2] [arg=132]: thread_a065e720 |start  ... <snip> 
..._sendActionsForEvents:withEvent:] | -[UIControl sendAction:to:forEvent:] | -
[UIApplication sendAction:to:from:forEvent:] | -[**AViewController navigateToB**] | 
+[NSObject alloc] | +[NSObject allocWithZone:] | _internal_class_createInstance | 
_internal_class_createInstanceFromZone | calloc | malloc_zone_calloc 

有人可以给我关于这里发生的事情的任何想法吗?在navigationToB方法中,释放bViewController后(在pushViewController之后),对于bViewController应该就是它了.甚至什么都不知道,因为它是对NavigationToB方法块而言是本地的,并且已经被释放.

Can someone please give me any ideas on what is happening here? In navigateToB method, once the bViewController is released (after pushViewController), that's should be it for bViewController. Nothing else even knows about it as it is local to the navigateToB method block and it has been released.

从B导航回A时,在viewDidLoad,viewWillAppear等中不会调用任何内容,而这些都会重新输入navigationToB.

When navigating from B back to A, nothing is invoked in viewDidLoad, viewWillAppear etc that will re-enter navigateToB.

看起来搜索显示控制器以某种方式引用了我的AViewController中的某个内容,因此当它自动发布时,它会将这个内容"记下来,但是我不知道这是怎么可能的,尤其是当我使用复制时在A和B之间传递数据.

It looks like somehow search display controller has a reference to something in my AViewController and so as it is autoreleased it is taking this "something" down with it but I cannot understand how this is possible, especially as I'm using copy to pass data between A and B.

我要对此上厕所.我敢肯定这是我的错误,所以我向您介绍Stack Overflow的传奇故事,以获取任何智慧之言或如何解决此问题的建议.

I'm going potty over this. I'm sure this is my mistake somewhere and so I turn to you, Stack Overflow legends for any words of wisdom or advice on how to resolve this.

非常感谢.

推荐答案

将视图控制器推到导航控制器上时,导航控制器将保留视图控制器.当该视图控制器弹出时,导航控制器将其释放.

When you push a view controller onto a navigation controller, the navigation controller retains the view controller. And when that view controller is popped, the navigation controller releases it.

UISearchDisplayController似乎正在尝试查看您的视图控制器是否响应选择器,并且由于它是从_destroyManagedTableView调用的,因此我猜选择器是searchDisplayController:willUnloadSearchResultsTableView :(您的视图控制器是搜索显示控制器的委托,对吗? ).

UISearchDisplayController appears to be trying to see if your view controllers responds to a selector, and since it's calling from _destroyManagedTableView, I'm guessing the selector is searchDisplayController:willUnloadSearchResultsTableView: (your view controller is the search display controller's delegate, correct?).

@robert-该示例一定是错误的,因为pushViewController确实保留了其参数.页面上的所有其他示例要么在初始化后立即自动释放,要么在推送后释放.

@robert - that example must be wrong, because pushViewController does retain its argument. All the other examples on the page either autorelease immediately after init, or release after pushing.

这篇关于UISearchDisplayController自动释放如何在其他视图控制器中导致崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆