NSMutableArray 删除对象会增加引用计数吗?
我有一些代码导致 iOS 静态库内存泄漏。这是来自 Instruments 的对象的生命周期:
# Category Event Type Timestamp RefCt Address Size Responsible Library Responsible Caller
0 Table_ColumnListener Malloc 4712087040 1 0x5e5fe60 16 Tests -[Table _initWithRows:andColumns:andTupleType:]
1 Table_ColumnListener Retain 4712301056 2 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
2 Table_ColumnListener Retain 4712453120 3 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
3 Table_ColumnListener Retain 4712602112 4 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
4 Table_ColumnListener Retain 4712755968 5 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
5 Table_ColumnListener Retain 4712915968 6 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
6 Table_ColumnListener Retain 4713062144 7 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
7 Table_ColumnListener Retain 4729644288 8 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
/\
This is what confuses me
8 Table_ColumnListener Release 4729646080 7 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
9 Table_ColumnListener Release 4729647872 6 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
10 Table_ColumnListener Retain 4729679104 7 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
11 Table_ColumnListener Release 4729680896 6 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
12 Table_ColumnListener Release 4729682944 5 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
13 Table_ColumnListener Retain 4729713152 6 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
14 Table_ColumnListener Release 4729714944 5 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
15 Table_ColumnListener Release 4729717248 4 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
16 Table_ColumnListener Release 4729731840 3 0x5e5fe60 0 Tests -[Table dealloc]
这是 [AbstractColumn addColumnListener:]
-(void) addColumnListener:(id <ColumnListener>)listener
{
[m_listeners addObject:listener];
}
和 [AbstractColumn removeColumnListener:]
-(void) removeColumnListener:(id <ColumnListener>)listener
{
[m_listeners removeObject:listener];
}
当我从 NSMutableArray 中删除对象时的 代码
,它似乎是增加保留计数而不是减少它。还有其他人遇到过这个问题并且知道解决方案吗?
I have some code that is causing memory leaks on an iOS static library. Here is an object's lifetime from Instruments:
# Category Event Type Timestamp RefCt Address Size Responsible Library Responsible Caller
0 Table_ColumnListener Malloc 4712087040 1 0x5e5fe60 16 Tests -[Table _initWithRows:andColumns:andTupleType:]
1 Table_ColumnListener Retain 4712301056 2 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
2 Table_ColumnListener Retain 4712453120 3 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
3 Table_ColumnListener Retain 4712602112 4 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
4 Table_ColumnListener Retain 4712755968 5 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
5 Table_ColumnListener Retain 4712915968 6 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
6 Table_ColumnListener Retain 4713062144 7 0x5e5fe60 0 Tests -[AbstractColumn addColumnListener:]
7 Table_ColumnListener Retain 4729644288 8 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
/\
This is what confuses me
8 Table_ColumnListener Release 4729646080 7 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
9 Table_ColumnListener Release 4729647872 6 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
10 Table_ColumnListener Retain 4729679104 7 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
11 Table_ColumnListener Release 4729680896 6 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
12 Table_ColumnListener Release 4729682944 5 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
13 Table_ColumnListener Retain 4729713152 6 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
14 Table_ColumnListener Release 4729714944 5 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
15 Table_ColumnListener Release 4729717248 4 0x5e5fe60 0 Tests -[AbstractColumn removeColumnListener:]
16 Table_ColumnListener Release 4729731840 3 0x5e5fe60 0 Tests -[Table dealloc]
Here is the code for [AbstractColumn addColumnListener:]
-(void) addColumnListener:(id <ColumnListener>)listener
{
[m_listeners addObject:listener];
}
And for [AbstractColumn removeColumnListener:]
-(void) removeColumnListener:(id <ColumnListener>)listener
{
[m_listeners removeObject:listener];
}
When I remove the object from the NSMutableArray
, it seems to increase the retain count instead of decrementing it. Has anyone else had this problem and knows of a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以重现您用仪器观察到的情况。在丢弃你的对象之前,NSMutableArray 会保留它,执行一些魔法,然后释放它。此释放抵消了之前的保留,并且与稍后发生的事情无关:再次释放,这次是因为对象已从数组中删除。
您根本不应该关心数组的内部实现正在做什么。它可以根据需要多次保留/释放对象,重要的是它比保留调用一次释放,因为您期望在删除对象时这样做。因此,您观察到的情况
很好,根本没有内存泄漏。在removeObject:的内部实现中执行代码时,retainCount可能会增加,但是一旦该方法返回,计数实际上就会减少。
I can reproduce what you are observing with Instruments. Before discarding your object, the NSMutableArray retains it, does some magic and then releases it. This release counteracts the previous retain and has nothing to do with what happens a bit later: again a release, this time because the object is removed from the array.
What the internal implementation of the array is doing should not concern you at all. It could retain/release the object as often as it wants, the important part is that it calls release once more than retain, because you expect that when the object is removed. Thus, what you observe
is just fine and no memory leak at all. While executing code in the internal implementation of removeObject:, the retainCount might rise, but as soon as the method returns, the count is actually decreased.