释放和自动释放有什么区别?
我对release和autorelease还有一些不清楚的理解。他们两者有什么区别呢?我有这个代码。用于 Facebook 连接。有时当我登录 Facebook 时会崩溃,我怀疑可能是因为我没有很好地释放对象。?感谢您的帮助
if (_session.isConnected) {
[_session logout];
} else {
FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:_session] autorelease];
[dialog show];
}
I still have some unclear understand about release and autorelease. What are the difference between both of them? I have this code. For facebook connection. I crash it sometimes when I go to Facebook login, I doubting maybe it is because I don't release the object nicely.? Thanks for any helps
if (_session.isConnected) {
[_session logout];
} else {
FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:_session] autorelease];
[dialog show];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Cocoa 内存管理编程指南很快就会成为您最好的朋友。简而言之,Cocoa 中的对象实例是使用引用计数来管理内存的(当然,除非您在 OS X 上使用垃圾收集)。一个对象表明它想要“保留”另一个实例的所有权权益——防止它被释放——通过向它发送一条
-retain
消息。对象通过向另一个实例发送-release
消息来表明它想要释放该兴趣。如果“保留”的对象数量和对象的所有权权益下降到 0(即,当最后一个拥有实例发送-release
消息时),则保留计数为 0 的实例为解除分配。有时可以方便地说“我希望在将来的某个时间发布此实例”。这就是
-autorelease
的目的。发送-autorelease
消息将接收者添加到当前的NSAutoreleasePool
中。当该池耗尽时,它会向池中的所有实例发送一条-release
消息。 NSAutoreleasePool 会在每个线程运行循环的每次迭代开始时自动创建,并在该迭代结束时耗尽。因此,您可以在方法中执行类似的操作:此方法的调用者将返回一个实例,如果他们希望保留该实例,他们可以
-retain
。如果他们不保留它,它至少会一直保留到封闭的自动释放池耗尽为止:The Memory Management Programming Guide for Cocoa will soon be your best friend. In brief, object instances in Cocoa are memory managed using reference counting (unless, of course you're using garbage collection on OS X). An object indicates that it wants to 'retain' an ownership interest in an other instance--keep it from being deallocated--by sending it a
-retain
message. An object indicates that it wants to release that interest by sending the other instance a-release
message. If the number of objects that have 'retained' and ownership interest in an object drops to 0 (i.e. when the last of the owning instances sends a-release
message), the instance with a 0 retain count is deallocated.It's sometimes convenient to say "I want this instance to be released some time in the future". That's the purpose of
-autorelease
. Sending an-autorelease
message adds the receiver to the currentNSAutoreleasePool
. When that pool is drained, it sends a-release
message to all the instances in the pool. AnNSAutoreleasePool
is automatically created at the start of each iteration of each thread's run loop and drained at the end of that iteration. Thus, you can do something like this in a method:The caller of this method will get back an instance that they can
-retain
if they wish to keep it. If they don't retain it, it will stick around at least until the enclosing autorelease pool is drained:释放意味着你立即释放它。
自动释放意味着您希望在下一个自动释放池中释放该变量。
当您想继续保留变量但又不想造成内存泄漏时,可以使用自动释放。当您不再需要该变量时,可以使用release。
示例:
为什么我们在那里使用自动释放?
如果我们使用[结果释放]来代替,变量结果将在那时被销毁。这意味着返回值将是垃圾。
如果我们根本不释放,变量
result
将永远保留,从而导致内存泄漏。我们可以告诉函数的每个调用者释放结果,但这会很令人头痛并且容易出错。
所以我们使用自动释放。我们标记要在下一个自动释放池中释放的变量。基本上我们在分配附近标记要释放的变量。因此,口头禅 alloc 始终与同一函数中的release 配对。
实际上,将所有发布更改为自动发布就可以了。您的内存使用效率不会很高,但是影响很小。所有编程语言中的所有变量都是有效自动释放的。
无论如何,使用ARC。
Releasing means you release that right away.
Autoreleasing means you want the variable to be released on the next autorelease pool.
You use autorelease when you want to keep retaining the variable but don't want to create a memory leak. You use release when you don't need the variable anymore.
Sample:
Why do we use autorelease there?
If we use [result release] instead, variable result will be destroyed AT that time. Which means that the returned value will be garbage.
If we do not release at all, variable
result
will be hold FOREVER incurring memory leak.We can tell every caller to the function to release result but that would be a headache and prone to error.
So we use autorelease. We mark the variable to be released on the next autorelease pool. Basically we mark the variable to be released near the alloc. Hence the mantra alloc is paired with release in the same function holds all the time.
Actually, you'll do fine changing all release into autorelease. Your memory use won't be efficient, however, the effect is minimal. All variables, in all programming language is effectively autoreleased.
Anyway, use ARC.
背景讨论:
objective-c 是引用计数的,因此当引用计数达到 0 时对象将被删除。release 立即减少引用计数,autorelease 在弹出 autorelease-pool 时减少引用计数
何时使用:
在分配对象时使用 autorelease if
使用“手动”释放
但真的是我的朋友:
埃里克
background discussion:
objective-c is reference counted, so objects are deleted when the reference count reaches 0. release reduces the reference-count immediately, autorelease reduces it when the autorelease-pool is popped
when to use:
use autorelease when allocating the object if
use "manual" release
but really my freand:
Erik
根据 Cocoa 内存管理编程指南:
还:
According to the Memory Management Programming Guide for Cocoa:
Also: