从 NSArray 中删除对象会自动释放其内存吗?
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
myClass *obj = [[myClass alloc] init];
NSArray *ar = [NSArray array];
[ar addObject: obj];
[ar removeObject: obj];
[pool drain];
从 NSArray 数组中删除一个对象会自动释放我之前分配的内存吗?从我从各种来源找到的答案来看,答案似乎是肯定的。问题是如果我测试内存泄漏,xcode 仍然抱怨 obj 尚未释放。那么到底发生了什么?
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
myClass *obj = [[myClass alloc] init];
NSArray *ar = [NSArray array];
[ar addObject: obj];
[ar removeObject: obj];
[pool drain];
Will removing an object from an NSArray array automatically release its memory that I have earlier allocated?? The answer seems to be yes from what I have found from various sources. The problem is if I test for memory leaks, xcode still complains that obj has not been released. So what's actually going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
集合保留您添加到其中的对象,声明临时所有权。当您从集合中删除某个项目时,它会释放该对象(及其临时声明)。换句话说,在将对象添加到集合之前和删除对象之后,保留计数将相同。
如果保留计数为 0,则回收内存。
在您的代码中,您分配一个对象并声明它的所有权。这意味着它的保留计数为 1。
然后将其添加到数组中。该数组保留该对象,获取临时所有权并将其保留计数增加到 2。
然后从数组中删除该对象。该数组释放该对象并放弃任何所有权声明,从而使保留计数回到 1。
由于在保留计数返回到 0 之前不会回收内存(没有人对该对象拥有声明),因此不会回收对象的内存。
如果您在将对象添加到数组之前自动释放了该对象,或者在删除该对象后调用了release(但不是两者!),则保留计数将为0并且内存将被回收。
Collections retain the objects you add to them, claiming temporary ownership. When you remove an item from the collection, it releases the object (and its temporary claim). In other words, the retain count will be the same before you add an object to a collection and after you remove it.
If that retain count is 0, the memory is reclaimed.
In your code you're allocating an object and claiming ownership of it. That means it has a retain count of 1.
Then you're adding it to the array. The array retains the object, taking temporary ownership and upping its retain count to 2.
You then remove the object from the array. The array releases the object and relinquishes any claim of ownership, bringing the retain count back down to 1.
Since memory is not reclaimed until retain count is back to 0 (nobody has a claim on the object), your object's memory is not reclaimed.
If you had autoreleased the object prior to adding it to the array, or called release on the object after you had removed it (but not both!), the retain count would be 0 and the memory would be reclaimed.
是的。当您将对象插入数组时,数组会保留该对象(增加其保留计数)。如果对象的保留计数为 1(即,其上没有其他保留),那么当将其从数组中删除时,保留计数将变为零并且可以删除。
但上述场景中的问题是,将对象添加到数组后,您未能释放该对象上的保留(由于 alloc/init)。在
[ar addObject:obj]
之后插入[obj release]
。(另请注意,在您的示例中,当您耗尽自动释放池时,整个数组将“噗”一下。)
Yes. When you insert an object into an array, the array retains it (bumps its retain count). If the object's retain count is 1 (ie, there are no other retains on it) then when it's removed from the array the retain count goes to zero and it's eligible to be deleted.
But your problem in the above scenario is that, after adding the object to the array, you failed to release YOUR retain on the object (due to the alloc/init). Insert
[obj release]
after the[ar addObject:obj]
.(Also note that in your example the entire array will go "poof" when you drain your autorelease pool.)
不,你分配它 ->保留计数为 1
将其添加到数组中,该数组将向对象发送另一个保留 -> 2
您从数组中删除对象,数组发送一个释放 -> 1
...所以现在保留计数回到1,这是您的初始分配保留,因此您需要释放它以释放内存。
No, you alloc it -> retain count of 1
You add it to the array which sends the object another retain -> 2
You remove the object from the array and the array sends a release -> 1
...so now the retain count is back to 1, which is your initial alloc retain, so you need to release it to free the memory.