何时以及为何必须使用发布?
我通过观看教程和尝试来学习 Objective-C 和 xcode,所以只是我自己。我从来没有真正跟老师上过真正的课或者类似的事情。而且我从来不知道 release
是用来做什么的。当然,我经常看到它,但我想了解一些关于它的事情。我是否必须对 - (void)dealloc
中的头文件中声明的每个对象使用release?如果我在 IBAction 中创建一个临时 NSString,我是否必须在操作结束时释放它?如果我不使用发布会发生什么?发布在编程语言中真正意味着什么?
I have learnes objective-c and xcode by watching tutorials and trying out, so just by myself. I never really had a real lesson with teachers or anything like that. And I never learnes what release
is used for. I have often seen it, of course but i want to know a few things about it. Do I have to use release for every object that i habe declared in the header file in - (void)dealloc
? If i create a temporary NSString in an IBAction do i have to release it at the end of the action? What happens if i dont use release? What does release really mean in programming language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
规则非常简单:如果您使用
new
、alloc
、retain
或copy 获取了对象,则只需释放该对象即可
(简称 NARC)。就是这样。一旦您使用完 NARC 对象并且不再需要它,您必须释放
它。如果您通过任何其他方式获取了该对象,则不得释放
它。如果您在类初始化的某个位置通过 NARC 获取了一个对象,并且需要在类实例的生命周期内保留该对象,则可以在
dealloc
方法中release
它。这将确保对象按照您需要的方式生存,并在类的实例消亡后消亡。在 Objective-C 中,每个实例都包含一个引用计数属性。当您使用 NARC 创建对象时,该引用计数为 1。每次连续的
retain
都会使引用计数增加 1。每次连续的release
都会使引用计数减少 1。当引用计数达到 0 时,对象将被释放。因此,如果您不释放 NARC 对象,则会出现内存泄漏(因为引用计数将保持在 0 以上,并且该对象永远不会被释放,即使您已经使用完它)。这意味着您的应用程序将拥有一个永远不会使用的内存空间。如果这些加起来,那么您将浪费大量宝贵的系统资源。在 iOS 上,这可能会导致您的应用程序被终止。
如果您需要返回使用 NARC 创建的对象,那么您应该
autorelease
它。这基本上意味着“稍后将调用该对象的release
”。因此,如果调用您的方法的方法需要保存您要返回的该对象,它可以保留
它,现在该对象的引用计数设置为2。release
将稍后会按计划调用,但对象被保留,其引用计数将变为 1,并且不会消亡。现在,调用者有责任在完成后对其调用release
。The rule is quite simple: you only need to release an object if you obtained it by using
new
,alloc
,retain
orcopy
(NARC for short). That's it. As soon as you're done with a NARC object and you don't need it anymore, you mustrelease
it. If you obtained the object though any other means, then you must notrelease
it.If you obtained an object by NARC somewhere in the class initialization and you need to keep this object during the lifetime of the class instance, then
release
it in thedealloc
method. This will make sure the object lives as much as you need and dies after the instance of the class dies.In Objective-C, each instance contains a reference count property. When you create an object with NARC, this reference count is 1. Each successive
retain
will increase the reference count by 1. Each successiverelease
will decrease the reference count by 1. When the reference count reaches 0, the object is deallocated.So, if you don't release NARC objects, then you will have a memory leak (because the reference count will remain above 0 and the object will never be deallocated, even if you're done with it). What this means is that your app will own a space in memory that it will never use. If these add up, then you'll be wasting lots of valuable system resources. On iOS this will likely get your app killed.
If you need to return an object created with NARC, then you should
autorelease
it. This basically means "the object will haverelease
called on it just a little later". So if the method calling your method needs to hold this object you're returning, it canretain
it, and now the object has the reference count set to 2.release
will be called a little later, as scheduled, but the object wasretain
ed, its reference count will go to 1, and it will not die. Now it's the caller's responsibility to callrelease
on it when it's done with it.您可能会受益于阅读 Objective-C 中内存管理的基本概念(分配、引用计数、保留/释放/自动释放...)。
Apple 在这里提供了很好的介绍。
这是一个很容易让新手和有经验的程序员感到困惑的主题,如果你能充分理解它,会对自己大有帮助。
这实在不是10行话能解释清楚的事情。但是开始阅读上面的文档,您将会更好地理解 Cocoa 中的内存管理。
You would probably benefit from reading up on the basic concepts of memory management in Objective-C (allocation, reference counting, retain/release/autorelease...).
Apple provides a nice introduction here.
It is a topic that can easily confuse both new and experienced programmers, and you would do yourself a great favor by getting a solid understanding of it.
It's really not something that can be explained in 10 lines. But start reading the above document and you will be on your way to a better understanding of memory management in Cocoa.