window addSubview释放问题

发布于 2024-11-25 15:01:24 字数 661 浏览 2 评论 0原文

我想知道有关我的应用程序的应用程序委托的一些信息。 为什么我不能像这样发布:

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沫雨熙 2024-12-02 15:01:24

其他答案是正确的,UIVIewController 没有被保留,我建议设置 UIWindows rootViewController(仅适用于 iOS 4.0 及更高版本)属性,该属性保留控制器。如果您的应用程序支持 iOS 4.0 之前的版本,那么您需要将控制器存储在实例变量中。

-(BOOL)application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    RootViewController *controller = [[RootViewController alloc]
                                      initWithNibName:@"RootViewController"
                                               bundle:[NSBundle mainBundle]];
    //controller will be retained and view will set for you
    window.rootViewController = controller;
    [controller release];
    [self.window makeKeyAndVisible];
    return YES;
}

The other answers are correct, the UIVIewController is not being retained, what I recommend is setting the UIWindows 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.

-(BOOL)application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    RootViewController *controller = [[RootViewController alloc]
                                      initWithNibName:@"RootViewController"
                                               bundle:[NSBundle mainBundle]];
    //controller will be retained and view will set for you
    window.rootViewController = controller;
    [controller release];
    [self.window makeKeyAndVisible];
    return YES;
}
回梦 2024-12-02 15:01:24

此行

[self.window addSubview:controller.view];

增加了 controller.view 的保留计数,而不是 controller。这就是为什么

[controller release];

会产生问题。

如果这是主窗口,那么您无需担心内存泄漏,因为窗口在程序的整个生命周期中都处于活动状态,并且在程序终止时所有内存都会被清除。

This line

[self.window addSubview:controller.view];

increases the retain count of controller.view not controller. That's why

[controller release];

creates 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.

等你爱我 2024-12-02 15:01:24

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.

想你只要分分秒秒 2024-12-02 15:01:24

当您将视图添加为子视图时,视图将被保留,而不是其控制器。因此,当您释放控制器时,它会被释放,而它的视图则不会。结果稍后视图尝试将消息发送到其已释放的控制器并且应用程序崩溃。

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.

前事休说 2024-12-02 15:01:24

这是因为您是该控制器的唯一所有者。您只需将其视图添加为窗口的子视图即可。尽管视图被窗口视图保留,但控制器却不然。

因此,它将被释放,任何进一步使用它都会使您的应用程序崩溃。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文