我可以提前释放自动释放对象吗?
即,如果我这样做,会导致对象立即被释放,而不必由池释放吗?
[[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当对象通常自动释放时,它可能会崩溃。
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.我现在意识到这很愚蠢,我不应该发布不属于我的东西。
I realise that this is stupid now, and that I shouldn't be releasing something I don't own.
如果您不希望对象进入自动释放池,您可以手动进行
alloc
和initWithCapabity
。如果您这样做,则必须在某个时候手动释放
它。If you don't want the object to go into the auto-release pool, you can do a manual
alloc
andinitWithCapabity
. If you do that, you'll have to manuallyrelease
it at some point.