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

查看:11
本文介绍了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 的定义如下(注意,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 的navigateToB 方法中发布的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 

有人可以就这里发生的事情给我任何想法吗?在navigateToB 方法中,一旦bViewController 被释放(在pushViewController 之后),就应该是bViewController.其他什么都不知道,因为它是navigateToB 方法块的本地函数,并且已经被释放.

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 等中不会调用任何将重新进入navigateToB 的内容.

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 确实保留了它的参数.页面上的所有其他示例要么在 init 后立即自动释放,要么在推送后释放.

@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天全站免登陆