自动释放对象的保留计数何时递减?
如果我有一个自动释放的对象,那么它的保留计数(即我对其调用方法retainCount时返回的值)何时递减?我认为这是当对象分配的方法的范围结束时,但我的测试并没有表明这一点。我有如下代码:
int itemIndex = 0;
NSArray* items = [mResponse componentsSeparatedByString:@","];
self.mText = (NSString*)[items objectAtIndex:itemIndex++];
self.mText = [mText gtm_stringByUnescapingFromURLArgument];
NSLog(@"retain count: %d",[mText retainCount]);
这将返回 2 作为计数(mText 有一个保留属性,因此这是有意义的),退出此方法后,来自外部的调用同样返回 2。
If I have an autoreleased object, when does its retain count (i.e. the value returned when I call the method retainCount on it) decrement? I had figured it to be when the scope of the method the object was allocated within ended, but my tests aren't indicating that. I have code like the following:
int itemIndex = 0;
NSArray* items = [mResponse componentsSeparatedByString:@","];
self.mText = (NSString*)[items objectAtIndex:itemIndex++];
self.mText = [mText gtm_stringByUnescapingFromURLArgument];
NSLog(@"retain count: %d",[mText retainCount]);
This returns 2 for the count (mText has a retain property so that makes sense), and after exiting this method, the call from outside likewise returns 2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要查看retainCount。这是误导性的。只要您遵循内存管理编程指南< /a> 那你就没事了。
如果您将问题改为“默认自动释放池何时耗尽?”那么答案就是当控制流返回到运行循环时。
Don't look at retainCount. It's misleading. As long as you follow the Memory Management Programming Guide then you'll be fine.
If you rephrase your question to say "When does the default autorelease pool drain?" then the answer is when control flow returns to the runloop.