ios - 这段代码是否过度发布?

标签 ios cocoa-touch release

我找不到问题出在哪里,但是这段代码崩溃了。我是否过度释放了任何物体?

settings = [[SettingsViewController alloc] initWithNibName:@"SettingsController" bundle:nil];
settings.hidesBottomBarWhenPushed = YES;

NSArray * arrayWithRootController = [[NSArray alloc] initWithObjects:settings, nil];
[(UINavigationController*)([self.tabBar.viewControllers lastObject])setViewControllers:arrayWithRootController];
[arrayWithRootController release];
[settings release];

如果我删除行

[settings release];

应用不会崩溃。但我很确定它是正确的。问题可能出在其他地方吗?

有什么想法吗?非常感谢

最佳答案

您在此片段中并没有过度释放,但显然有些地方不对。将 Root View Controller 添加到数组将保留它,但仅在数组的生命周期内。当数组结束时,其中的所有对象也会被释放(我猜测这里发生了什么)。

尝试一些事情: 首先,确保将阵列放在您认为的位置:

[(UINavigationController*)([self.tabBar.viewControllers lastObject])setViewControllers:arrayWithRootController];

您期望 [self.tabBar.viewControllers lastObject] 是什么?您确定这是您要分配新阵列的地方吗?如果接收器无效,您的数组将不会保留在下一行中,这意味着 View Controller 也将被释放。

我不熟悉您的体系结构,但您似乎正在将一组 View Controller 分配给一个 View Controller 。 self.tabBar 是一个导航 Controller ,您可以在其上调用setViewControllers。但是 self.tabBar.viewControllers lastObject... 很可能这是一个 View Controller ,但不一定是导航 Controller ,它可能不会响应 setViewControllers(在这种情况下它应该崩溃,除非它是 nil,这我猜它可能是)。

最后,尝试使用 Instruments 工具 Zombies 看看是否可以查明意外释放的来源。

关于ios - 这段代码是否过度发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14984991/

相关文章:

android - 使用 React Native 时,我有哪些存储数据的选项? (iOS 和安卓)

ios - 无需翻译即可在多个商店分发 iOS 应用程序

objective-c - 如何判断触控板点击的NSEvent,而不是点击点击

git - 远程计算机上 git 中的多个包文件

iphone - 应用程序在 xcode 中释放数组对象时崩溃

ios - 如何在 iOS 11 中将导航栏设置为透明

objective-c - iOS:NSUserDefaults 和 UIImageViews

iphone - 核心数据 : Fetch result from multiple entities or relationship

ios - 基础类(class)的扩展

Android 生成发布 apk,任务 ':app:transformClassesAndResourcesWithProguardForProductionRelease' 失败