我可以提前释放自动释放对象吗?

发布于 2024-08-31 00:28:52 字数 130 浏览 12 评论 0原文

即,如果我这样做,会导致对象立即被释放,而不必由池释放吗?

[[NSArray arrayWithCapacity:100] release];

在文档中找不到关于此的明确解释。

i.e. would cause the object to be released immediately and not have to be released by the pool if I did this?

[[NSArray arrayWithCapacity:100] release];

Can't find a clear explanation in the docs about this.

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

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

发布评论

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

评论(3

许久 2024-09-07 00:28:52

当对象通常自动释放时,它可能会崩溃。 autorelease 的意思是“延迟发布”,所以会发布:稍后。由于该对象稍后在您手动释放它时将不存在,因此您可能会由于运行时向您现在释放的对象发送 -release 消息而崩溃。

编辑:请注意,如果您 -retain 自动释放的对象,则必须 -release 它们:您正在获取所有权。

It would likely crash when the object would normally be autoreleased. autorelease means "delayed release", so it will be released: just later. Since the object won't exist later as you are manually releasing it, you will likely crash due to the runtime sending the -release message to your now-deallocated object.

Edit: Note that if you -retain objects that come autoreleased, you do have to -release them: you are taking ownership.

任性一次 2024-09-07 00:28:52

我现在意识到这很愚蠢,我不应该发布不属于我的东西。

I realise that this is stupid now, and that I shouldn't be releasing something I don't own.

风月客 2024-09-07 00:28:52

如果您不希望对象进入自动释放池,您可以手动进行allocinitWithCapabity。如果您这样做,则必须在某个时候手动释放它。

If you don't want the object to go into the auto-release pool, you can do a manual alloc and initWithCapabity. If you do that, you'll have to manually release it at some point.

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