window addSubview释放问题
我想知道有关我的应用程序的应用程序委托的一些信息。 为什么我不能像这样发布:
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
RootViewController *controller = [[RootViewController alloc]
initWithNibName:@"RootViewController"
bundle:[NSBundle mainBundle]];
[self.window addSubview:controller.view];
[controller release]; // Here's my question
[self.window makeKeyAndVisible];
return YES;
}
我几乎可以肯定 -addSubview
方法会使我的保留计数增加 1。那么为什么当我释放控制器时会发生崩溃呢?为什么它在除代表之外的另一个班级中工作?
谢谢 !
I was wondering something about the app delegate of my app.
Why can't I release like this :
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
RootViewController *controller = [[RootViewController alloc]
initWithNibName:@"RootViewController"
bundle:[NSBundle mainBundle]];
[self.window addSubview:controller.view];
[controller release]; // Here's my question
[self.window makeKeyAndVisible];
return YES;
}
I was almost sure that -addSubview
method increase by 1 my retain count. So why do I have crash when I release my controller ? Why is it working in another class but the delegate ?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
其他答案是正确的,UIVIewController 没有被保留,我建议设置
UIWindow
s rootViewController(仅适用于 iOS 4.0 及更高版本)属性,该属性保留控制器。如果您的应用程序支持 iOS 4.0 之前的版本,那么您需要将控制器存储在实例变量中。The other answers are correct, the UIVIewController is not being retained, what I recommend is setting the
UIWindow
s rootViewController (only available iOS 4.0 and later) property which does retain the controller. If your app supports pre iOS 4.0 then you will need to store controller in an instance variable.此行
增加了
controller.view
的保留计数,而不是controller
。这就是为什么会产生问题。
如果这是主窗口,那么您无需担心内存泄漏,因为窗口在程序的整个生命周期中都处于活动状态,并且在程序终止时所有内存都会被清除。
This line
increases the retain count of
controller.view
notcontroller
. That's whycreates a problem.
If this is the main window, then you don't need to worry about the memory leak, because the
window
is active for the entire life of the program, and all memory is purged when it terminates.addSubView
增加了视图控制器内视图的保留计数,这就是如果释放控制器应用程序崩溃的原因。无论如何,不释放就会有泄漏。解决方案是在类中创建一个 ivar 并为其分配视图控制器(而不是局部变量),然后在dealloc 中释放它。
addSubView
increases the retain count of the view inside of the view controller, that is why the app crashes if you release the controller.in any case, if you don't release it, you will have a leak. the solution is creating an ivar in your class and assigning to it the view controller (instead of a local variable), then release it in
dealloc
.当您将视图添加为子视图时,视图将被保留,而不是其控制器。因此,当您释放控制器时,它会被释放,而它的视图则不会。结果稍后视图尝试将消息发送到其已释放的控制器并且应用程序崩溃。
When you add view as subview then view gets retained, not its controller. So when you release controller it gets deallocated and its view - not. As result later view try to send messages to its already deallocated controller and app crashes.
这是因为您是该控制器的唯一所有者。您只需将其视图添加为窗口的子视图即可。尽管视图被窗口视图保留,但控制器却不然。
因此,它将被释放,任何进一步使用它都会使您的应用程序崩溃。
This because you are the unique owner of that controller. You just add its view as a subview of the window. Although the view gets retained by the window's view, the controller not.
So, it will get deallocated and any further use of it will make your app crash.