复制对象时的内存管理
我知道我的问题已经在 StackOverflow 上讨论过,但我发现答案不完整,无法满足我的需求。所以问题是:
NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects: obj1,obj2,nil];
NSMutableArray *secondArray = [[NSMutableArray alloc] init];
secondArray = [firstArray mutableCopy];
现在第二个数组的保留计数是多少? 2还是1?我应该释放它两次还是只释放一次? copy 或 mutableCopy 是否会增加 COPYING(此事件中的 secondaryArray)对象的保留计数?
I know that my question has already been discussed on StackOverflow but i found the answer not complete for my needs. So the question is:
NSMutableArray *firstArray = [[NSMutableArray alloc] initWithObjects: obj1,obj2,nil];
NSMutableArray *secondArray = [[NSMutableArray alloc] init];
secondArray = [firstArray mutableCopy];
what is retain count for the secondArray now? 2 or 1? Should i release it twice or just once?
Does copy or mutableCopy increases retain count of the COPYING (secondArray in this event) object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您永远不应该关心绝对保留计数。只是你是“平衡的”,这意味着对于每个
alloc
、new*
、copy
、mutableCopy
和retain
您需要相应的release
或autorelease
(即不使用ARC时)。如果将此规则应用于每一行,您可以看到第二行有一个分配,但没有释放。事实上,在这里分配一个实例是绝对没有用的,因为无论如何你对它都不感兴趣。所以它应该简单地读为:
但是让我们讨论一下你的原始代码,看看发生了什么:
在 Objective-C 中,我们经常谈论所有权:很少有方法可以让你成为对象的“所有者” 。它们是:
alloc
new*
,如newFoo
copy
和mutableCopy
retain
如果您调用这些,您将获得一个您负责的对象。这意味着您需要对这些对象调用相应数量的
release
和/或autorelease
。例如,如果您执行[[obj keep] keep];
然后[[obj autorelease] release];
就可以了You should never care about the absolute retain count. Only that you're "balanced", that means for every
alloc
,new*
,copy
,mutableCopy
andretain
you need a correspondingrelease
orautorelease
(when not using ARC, that is).If you apply this rule to each line you can see that your second line has an
alloc
, but there's no release. In fact, it's absolutely useless to allocate an instance here since you're not interested in it anyway. So it should simply read:But let's discuss your original code and see what happened:
In Objective-C, we often speak of ownership: there are very few methods that make you the "owner" of an object. These are:
alloc
new*
, as innewFoo
copy
andmutableCopy
retain
If you call these, you get an object for which you are responsible. And that means you need to call a corresponding number of
release
and/orautorelease
on these objects. For example, you're fine if you do[[obj retain] retain];
and then[[obj autorelease] release];
发生的情况是您造成了内存泄漏。当您使用此行的firstArray 的mutableCopy 覆盖它时,您刚刚丢失了分配给secondArray 的引用。
如果您随后释放 secondaryArray 两次,程序将崩溃,因为您过度释放了由 分配的可变数组。
您需要做的是确保您没有错误地覆盖保留的引用,并且平衡保留与释放。
What is happening is that you've created a memory leak. You just lost the reference assigned to secondArray when you overwrote it with the mutableCopy of firstArray with this line.
If you then release secondArray twice, the program will crash because you're then overreleasing the mutable array assigned by
What you need to do is to make sure you're not overwriting retained references by mistake, and balance retains with releases.