简体   繁体   English

Alloc init保留吗?

[英]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 如果releaseautorelease对象,则其保留计数减少1

In your example code, both alloc and retain increase the retain count by 1. 在示例代码中, allocretain将保留计数增加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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM