为什么释放视图控制器会导致崩溃?

发布于 2024-11-11 16:27:03 字数 485 浏览 4 评论 0原文

我总是像这样将一个新的视图控制器推送到堆栈上:

 MyViewController *vc = [[MyViewController alloc] init];

[self.navigationController pushViewController:vc animated:YES];

[vc release];

当将其从堆栈中弹出时,一切正常:

[self.navigationController popViewControllerAnimated:NO];

但是现在,当我将 vc 从堆栈中弹出时,我在 main.m 中崩溃,指出访问错误在行:int retVal = UIApplicationMain(argc, argv, nil, nil);

但现在如果我注释掉 [vc release] 就不会再崩溃了?

但是为什么这肯定会泄漏内存,因为我没有释放我创建的东西?

I always push a new view controller onto the stack like this:

 MyViewController *vc = [[MyViewController alloc] init];

[self.navigationController pushViewController:vc animated:YES];

[vc release];

And all works well when it comes to popping it off the stack with:

[self.navigationController popViewControllerAnimated:NO];

But now when I pop the vc off the stack I get a crash in main.m stating a bad access at line:int retVal = UIApplicationMain(argc, argv, nil, nil);

But now if I comment out [vc release] no more crash?

But why and surely this leaks memory as Im not releasing something I created?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

卖梦商人 2024-11-18 16:27:03

你的内存管理看起来不错。也许您对 vc 内部某些内容的内存管理不善。 MyViewController 的 dealloc 方法是什么样的?

我的猜测是您使用了不正确的 init 方法(可能是 initWithNibName:bundle:),并且您在 dealloc 中释放了从未正确初始化的 ivars。

Your memory management looks fine. Perhaps you are mismanaging the memory of something inside of your vc. What does the dealloc method of MyViewController look like?

My guess is you are using the incorrect init method (perhaps initWithNibName:bundle:) and you are releasing ivars in dealloc that were never properly initialized.

烟酒忠诚 2024-11-18 16:27:03

您是否尝试过将其用作

@property

Did you try using it as a

@property
巾帼英雄 2024-11-18 16:27:03

导航控制器将保留 vc,然后,当 vc 弹出时,navigationController 释放它并 vc 释放。

所以,你必须留下发布代码,它是正确的。

我认为你必须使用 initWithNibName:bundle: 代替 init

Navigation controller will retain vc then, when vc is popped, navigationController releases it and vc deallocs.

So, you must leave the release code, it is correct.

I think you have to use a initWithNibName:bundle: insted of the init.

对不⑦ 2024-11-18 16:27:03

它们不同的原因是您没有分配文本对象,因此您不是所有者。分配和释放它们是 IB 的工作,它也确实做到了。

所以如果你也尝试释放它,就会出现问题。

The reason why they are different is that you are not allocating the text objects, and therefore you are not the owner. It is the IB's job to alloc and realese them, which it does.

So if you also try to release it, it will cause problems.

苏辞 2024-11-18 16:27:03

也许您对 vc 内部某些内容的内存管理不善。

@brandontreb 的这句话真的对我很有帮助!在“收到模拟内存警告”之后,我花了一整天的时间来修复崩溃,具体描述如下:
防止 uinavigationcontroller 设置中 popViewControllerAnimated 的错误访问崩溃

在我推送的视图控制器的 loadView: 中,将视图控制器自身传递给其数据源的 init :.

LayoutPickerDataSource *pickerDataSource = [[LayoutPickerDataSource alloc] initWithController:self];

而 dataSource 类保留它,如:

@property (nonatomic, retain) LayoutViewController *viewController;

修复崩溃只需更改为:

@property (nonatomic, assign) LayoutViewController *viewController;

并删除:

[viewController release];

宾果!我还是不知道为什么!作为viewController在dataSource的dealloc:中释放。

Perhaps you are mismanaging the memory of something inside of your vc.

This sentence from @brandontreb really helped me! I've struggled a whole day to fix the crash after a 'Received simulated memory warning', exactly descripted like:
Preventing bad access crash for popViewControllerAnimated in uinavigationcontroller setup

In my pushed view controller's loadView:, passed the view controller self to its dataSource's init:.

LayoutPickerDataSource *pickerDataSource = [[LayoutPickerDataSource alloc] initWithController:self];

while the dataSource class retained it like:

@property (nonatomic, retain) LayoutViewController *viewController;

Fix the crash just change to:

@property (nonatomic, assign) LayoutViewController *viewController;

and remove:

[viewController release];

bingo! I still don't know why! As viewController released in dealloc: of dataSource.

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