自动释放初学者 [英] Autorelease for beginners

查看:124
本文介绍了自动释放初学者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个非常实用的定义这里


当你自动释放时,你基本上说:我不需要这个了,但任何人都可以自由地拿起释放池被耗尽)。当你显式地更新一个对象,你说:我不需要这个了,除非任何人已经说过(获得),它应该立即释放。



因此,autorelease不是
通常是错误的。当你想将对象
传递回消息的发送者
时,需要
,而不要求发送者采用
释放对象。


但是,我仍然想知道第二段是什么意思。自动释放通常不是错误的事情做...我认为这是真的内存密集型,所以它应该自然是错误的事情做。但是对于最后一句话,恐怕我不能得到它。



什么时候真的需要使用autorelease,什么是一个很好的例子/规则

解决方案

感谢任何建议。


自动释放通常不是错误的
做的事情...我认为这真的是
内存密集型,所以应该
自然是错误的事情do。


这不是内存密集型。如果您分配和自动释放大量对象(例如在循环中)或较少数量的大对象,则可能会遇到问题。但是autorelease只是延迟你正在创建的对象的释放,并且当释放确实发生时可能甚至不会被释放(自动释放的对象也可能被其他对象保留)。



如果你可以立即释放一个对象,那么。如果你需要自动释放,那么这样做,不要担心内存。如果你在循环的每次迭代中创建大量对象,你可能要考虑使用自己的autorelease池,或者使用alloc / init创建这些对象,以便你可以立即释放它们。


但是对于最后一句话,恐怕我不能得到它。


如果方法必须在返回之前释放它创建的每个对象,那么没有办法返回对象而不需要调用者释放返回的对象。自动释放允许一个方法创建一个对象并释放它,但推迟该释放,直到调用者有机会使用并可能保留返回的对象。


I found this very useful definition here:

When you autorelease, you're basically saying: "I don't need this any longer, but anyone else is free to pick it up (before the auto release pool is drained)". When you explicitly relase an object you're saying: "I don't need this any longer and unless anyone else has already said otherwise (acquired), it should be deallocated immediately."

Consequently, autorelease is not normally the wrong thing to. It is required when you want to pass objects back to the sender of a message without requiring the sender to take care of releasing the object.

However, I am still wondering what the second paragraph means. Autorelease is not normally the wrong thing to do... I thought it's really memory intensive, so it should naturally be the wrong thing to do. But as for the last sentence, I'm afraid I don't get it.

When do I really need to use autorelease and what would be a good example / rule of thumb that beginners can easily remember?

Thanks for any suggestions.

解决方案

Autorelease is not normally the wrong thing to do... I thought it's really memory intensive, so it should naturally be the wrong thing to do.

It's not "memory intensive" at all. If you allocate and autorelease a very large number of objects, such as in a loop, or a smaller number of large objects, you could run into problems. But autorelease just delays the release of objects that you're creating anyway, and which may or may not even be deallocated when the release does happen (the autoreleased objects may also be retained by other objects).

If you can release an object immediately, do that. If you need to autorelease, then do that and don't worry about the memory. If you're creating lots of objects in each iteration of a loop, you might want to consider either using your own autorelease pool or creating said objects with alloc/init so that you can release them immediately.

But as for the last sentence, I'm afraid I don't get it.

If a method had to release every object that it created before returning, there'd be no way to return an object without requiring the caller to release the returned object. Autorelease allows a method to create an object and release it, but to defer that release until the caller has had a chance to use and possibly retain the returned object.

这篇关于自动释放初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆