【发布时间】:2010-05-03 14:04:25
【问题描述】:
我有两个视图控制器 A 和 B。我从 A 导航到视图控制器 B,如下所示:
// 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 和其他属性):
@property (nonatomic, copy) NSString *bProperty1;
在 A 和 B 之间来回导航时,一切似乎都运行良好。直到我将 UISearchDisplayController 添加到 BViewController 中包含的表视图中。现在,当我从 B 导航到 A 时,应用程序崩溃了。
堆栈跟踪显示了崩溃时自动释放的搜索显示控制器的外观:
#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 节目:
-[BViewController respondsToSelector:]: message sent to deallocated instance 0x406c1e0
并且关于这个的 malloc 历史指向已经在上面 A 的 navigateToB 方法中释放的 bViewController:
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 方法块的本地并且已被释放。
当从 B 导航回 A 时,不会在 viewDidLoad、viewWillAppear 等中调用任何会重新进入 navigateToB 的内容。
看起来搜索显示控制器以某种方式引用了我的 AViewController 中的某些内容,因此由于它是自动释放的,因此它正在将这个“某些东西”拿下来,但我不明白这是怎么可能的,尤其是当我使用副本时在 A 和 B 之间传递数据。
我要为此而烦恼。我确定这是我在某个地方的错误,所以我求助于 Stack Overflow 的传奇人物,寻求任何关于如何解决这个问题的智慧或建议。
非常感谢。
【问题讨论】:
标签: objective-c iphone-sdk-3.0 uiviewcontroller