简体   繁体   English

iOS5中的viewController对象BAD_ACCESS?

[英]viewController object BAD_ACCESS in iOS5?

Usually when we push viewcontroller, we will create object for view controller, after that line we will push and release it. 通常,当我们推viewcontroller时,我们将为视图控制器创建对象,在那一行之后,我们将推并释放它。
But it gets crashed when we run in iOS5. 但是当我们在iOS5中运行时,它崩溃了。 So I retained the object through propery and declare it in interface as global. 因此,我通过属性保留了该对象,并在接口中将其声明为全局对象。 Now it is working fine. 现在工作正常。 Will retaining viewcontroller occupy much memory? 保留ViewController会占用大量内存吗? What is the difference between following two approches? 以下两个方法有什么区别?

One: 一:

MyViewCOntroller *obj = [[MyViewCOntroller  alloc] init];
[self.navigationController pushViewController:obj Animated:YES];
[obj Release]

Two: 二:

self.obj = [[MyViewCOntroller  alloc] init];
[self.navigationController pushViewController:self.obj Animated:YES];
[self.obj Release]

The first one should be right and please detect for the crash reason again. 第一个应该是正确的,请再次检测崩溃原因。 It can't be crashed when you use the first one to push a new view controller. 当您使用第一个推送新的视图控制器时,它不会崩溃。

As for the difference: in the second one, if you declare the obj as a var of self class and you don't use ARC , you take care of obj like the other instance vars. 至于区别:在第二个例子中,如果您将obj声明为self类的var,并且不使用ARC ,则您将像其他实例vars一样处理obj You just need to do release in the dealloc . 您只需要在dealloc释放。

Generally speaking, you should not release property (self.obj) in methods except for dealloc . 一般来说,除了dealloc之外,您不应该在方法中释放property (self.obj)。
The second code snippet should be replaced like this: 第二个代码段应该像这样替换:

self.obj = [[MyViewCOntroller alloc] init];
[self.navigationController pushViewController:self.obj Animated:YES];

And add below one to your dealloc method: 并将以下一项添加到您的dealloc方法中:

self.obj = nil; // Property will release itself and set the point to nil

The first code snippet is OK, you alloced local instance and released it after used. 第一个代码段可以,您分配了本地实例并在使用后释放了它。

why aren't you init your view controller with nib? 为什么不使用nib初始化视图控制器?

    SearchView *secondViewController = [[SearchView alloc] initWithNibName:@"SearchView" bundle:nil];
    [self.navigationController pushViewController:secondViewController animated:YES];
    [secondViewController release];

be careful with retaining any objects. 小心保留任何物体。 You must be completely sure that you init it only onse and then release it. 您必须完全确保只在一次初始化它,然后释放它。 If you do so, you may not care about memory. 如果这样做,您可能不会在乎内存。 The difference between your inits is: in 1st case you creating ner object. 您的init之间的区别是:在第一种情况下,您将创建ner对象。 It is NOT retain, but, may be leak, I'm not shure. 它不保留,但可能是泄漏,我不知道。 I think you should add autorelease . 我认为您应该添加autorelease In 2nd case you have an object's property (probebly, retain?) in header. 在第二种情况下,标头中有一个对象的属性(可能是保留吗?)。 you must release it in dealloc method 您必须在dealloc方法中释放它

Are you passing the View Controller to the new Object? 您是否正在将视图控制器传递给新对象? If yes, Are you releasing this View Controller property in the new View Controller's dealloc method? 如果是,是否在新的View Controller的dealloc方法中释放此View Controller属性? That would be a double release. 那将是双重发行。 Example no. 范例编号 2 would solve this problem because of the (retain) type property it may have in the old View Controller, would set its retain count to 2. 2将解决此问题,因为它可能在旧的View Controller中具有(retain)type属性,因此将其保留计数设置为2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM