谁能解释一下这个保留计数代码中发生了什么?
NSMutableString *ms = [[NSMutableString alloc]init];
[ms appendFormat:@"element %ld",1];
[ms appendFormat:@"element %ld",2];
NSMutableString *ms2 = [ms mutableCopy];
NSLog(@"ms retain count:%lu",ms.retainCount);
NSLog(@"ms2 retain count:%lu",ms2.retainCount);
NSValue *sw = [NSValue valueWithNonretainedObject:ms2];
NSMutableArray *a = [NSMutableArray array];
[a addObject:ms];
[a addObject:sw];
NSLog(@"ms retaincount %lu",ms.retainCount);
NSLog(@"ms2 retaincount %lu",ms2.retainCount);
NSMutableString *ms = [[NSMutableString alloc]init];
[ms appendFormat:@"element %ld",1];
[ms appendFormat:@"element %ld",2];
NSMutableString *ms2 = [ms mutableCopy];
NSLog(@"ms retain count:%lu",ms.retainCount);
NSLog(@"ms2 retain count:%lu",ms2.retainCount);
NSValue *sw = [NSValue valueWithNonretainedObject:ms2];
NSMutableArray *a = [NSMutableArray array];
[a addObject:ms];
[a addObject:sw];
NSLog(@"ms retaincount %lu",ms.retainCount);
NSLog(@"ms2 retaincount %lu",ms2.retainCount);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是您期望
retainCount
有用。事实并非如此,您应该忘记
retainCount
存在但发生的情况是这样的:
您创建了一个可变字符串。 您拥有它并负责
释放
它您将一些数据附加到字符串中。所有权没有变化。
您创建该字符串的副本。 您拥有该副本并负责
释放
它您将指向字符串副本的指针存储在
NSValue
中。您不拥有NSValue
(因此不必释放
它),并且由于您使用的是NonretainedObject:
变体,ms2
对象的所有权不变。您创建一个可变数组。你不拥有它。
您将一个对象添加到数组中。 数组现在也拥有该对象
您将一个对象添加到数组中。 数组现在拥有该对象(您仍然不拥有它)
因此,在此代码的末尾,您拥有:
ms
ms2
这意味着为了使您的代码正确,您还应该:
编辑:
您如何知道何时“拥有”一个对象以及何时不“拥有”一个对象?这非常简单:
alloc
”开头的方法检索对象,或者...new<”开头的方法检索对象/code>”或...
copy
”的方法检索对象,或者...retain
”该对象如果您显式 请记住:New-Alloc-Retain-Copy(“NARC”)。如果您满足这四个条件之一(并且文档/方法声明没有另外说明),那么您“拥有”该对象,并且必须通过调用
release
或autorelease< 来放弃该所有权/code> 在该对象上。
这一切都在内存管理编程中非常清楚地阐述指南。
Your problem is that you're expecting
retainCount
to be useful.IT IS NOT, AND YOU SHOULD FORGET THAT
retainCount
EXISTSBut here's what happens:
You have created a mutable string. You own it and are responsible for
releasing
itYou append some data to the string. No change in ownership.
You create a copy of the string. You own the copy and are responsible for
releasing
itYou store the pointer to your string copy in an
NSValue
. You do not own theNSValue
(and thus do not have torelease
it), and since you're using theNonretainedObject:
variant, the ownership of thems2
object is unchanged.You create a mutable array. You do not own it.
You add an object to the array. The array now also owns the object
You add an object to the array. The array now owns the object (you still do not own it)
So at the end of this code, you own:
ms
ms2
This means that for your code to be correct, you should also have:
Edit:
How do you know when you "own" an object and when you do not? It's pretty simple:
alloc
" or...new
" or...copy
" or...retain
" the objectJust remember: New-Alloc-Retain-Copy ("NARC"). If you satisfy one of those four conditions (and the documentation/method declaration doesn't say otherwise), then you "own" the object and must relinquish that ownership by invoking
release
orautorelease
on that object.This is all very plainly laid out in the Memory Management Programming Guide.
*) 请参阅 内存管理规则
*) See the memory management rules