[英]Alloc init retain?
Can anybody explain why the retain count of this line of code: 谁能解释为什么这行代码的保留计数:
[[[SomeClass alloc] init] retain]
Is two? 是两个吗 I'm only calling retain once.
我只叫保留一次。
Because alloc adds 1 to the retain count. 因为alloc给保留计数加1。 You should read Apple's Memory Management Programming Guide .
您应该阅读Apple的《 内存管理编程指南》 。
The retain count is two because [alloc]
also increases the retain count. 保留计数为2,因为
[alloc]
也增加了保留计数。 Strictly speaking the retain count of an object isn't just how many times you've called retain, but how many things are holding on to it. 严格来说,对象的保留计数不仅是您调用过的保留次数,还包括保留了多少东西。
If you create an object, using a method who's name begins with alloc, new, copy or mutableCopy
, or you retain
an object, its retain count is increased by 1 如果创建对象,则使用名称以
alloc, new, copy or mutableCopy
开头的方法,或者retain
对象,则其保留计数增加1
If you release
or autorelease
an object, it's retain count decreases by 1 如果
release
或autorelease
对象,则其保留计数减少1
In your example code, both alloc
and retain
increase the retain count by 1. 在示例代码中,
alloc
和retain
将保留计数增加1。
See Apple's docs for further details: 有关更多详细信息,请参阅Apple的文档:
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.