开发者

NSMutableArray remove object increases reference count?

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_C开发者_如何学运维olumnListener 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?


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

retain
release
release

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜