0

我假设NSArrayController's addObject 和 removeObject 的工作方式类似于 NSMutableArray 中的类似物。

但是,看起来 addObject 将其目标对象的引用计数增加了 3,而NSArrayController:removeObject将目标的引用计数仅减少了 1,留下了 2 的差异,这导致了我的代码中的内存泄漏。

NSArrayController *ac = [[NSArrayController alloc] init];
NSMutableArray *ma = [[NSMutableArray alloc] initWithCapacity:3];
Custom *obj = [[Custom alloc] init];
NSLog(@"Initial: %lu", [obj retainCount]);

[ma addObject:obj];
NSLog(@"After NSMutableArray:addObject: %lu", [obj retainCount]);
[ma removeObject:obj];
NSLog(@"After NSMutableArray:removeObject: %lu", [obj retainCount]);

[ac addObject:obj];
NSLog(@"After NSArrayController:addObject: %lu", [obj retainCount]);
[ac removeObject:obj];
NSLog(@"After NSArrayController:removeObject: %lu", [obj retainCount]);

[obj release];
NSLog(@"End: %lu", [obj retainCount]);

我期望看到的:

2013-02-10 23:11:22.344 Demo[27208:303] Initial: 1
2013-02-10 23:11:22.345 Demo[27208:303] After NSMutableArray:addObject: 2
2013-02-10 23:11:22.345 Demo[27208:303] After NSMutableArray:removeObject: 1
2013-02-10 23:11:22.345 Demo[27208:303] After NSArrayController:addObject: 2
2013-02-10 23:11:22.345 Demo[27208:303] After NSArrayController:removeObject: 1
2013-02-10 23:11:22.345 Demo[27208:303] End: 0

我实际看到的:

2013-02-10 23:11:22.344 Demo[27208:303] Initial: 1
2013-02-10 23:11:22.345 Demo[27208:303] After NSMutableArray:addObject: 2
2013-02-10 23:11:22.345 Demo[27208:303] After NSMutableArray:removeObject: 1
2013-02-10 23:11:22.345 Demo[27208:303] After NSArrayController:addObject: 4
2013-02-10 23:11:22.345 Demo[27208:303] After NSArrayController:removeObject: 3
2013-02-10 23:11:22.345 Demo[27208:303] End: 2

我只是使用NSArrayController不正确吗?


编辑

以下是 Instruments 的结果。Instruments 说该对象最终会进入一个自动释放池,最终会被 ArrayController 耗尽。据我了解,自动释放池在运行循环结束时被耗尽。但是我的dealloc方法永远不会在我的演示代码中被调用,即使我手动添加了一个NSAutoreleasePool并耗尽它。

NSMutableArray

代码

[ma addObject:obj];
NSLog(@"After NSMutableArray:addObject: %lu", [obj retainCount]);
[ma removeObject:obj];
NSLog(@"After NSMutableArray:removeObject: %lu", [obj retainCount]);

轮廓

#   Address Category    Event Type  RefCt   Timestamp   Size    Responsible Library Responsible Caller
0   0x7fc02c80d170  Custom  Malloc  1   00:00.363.486   16  Demo    -[AppDelegate applicationDidFinishLaunching:]
1   0x7fc02c80d170  Custom  Retain  2   00:00.364.189   0   Demo    -[AppDelegate applicationDidFinishLaunching:]
2   0x7fc02c80d170  Custom  Retain  3   00:00.364.502   0   Demo    -[AppDelegate applicationDidFinishLaunching:]
3   0x7fc02c80d170  Custom  Release 2   00:00.364.503   0   Demo    -[AppDelegate applicationDidFinishLaunching:]
4   0x7fc02c80d170  Custom  Release 1   00:00.364.505   0   Demo    -[AppDelegate applicationDidFinishLaunching:]
5   0x7fc02c80d170  Custom  Release 0   00:00.364.852   0   Foundation  -[NSNotificationCenter postNotificationName:object:userInfo:]

NSArrayController

代码

// NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[ac addObject:obj];
NSLog(@"After NSArrayController:addObject: %lu", [obj retainCount]);
[ac removeObject:obj];
NSLog(@"After NSArrayController:removeObject: %lu", [obj retainCount]);

// [pool drain];

轮廓

#   Address Category    Event Type  RefCt   Timestamp   Size    Responsible Library Responsible Caller
0   0x7ff1e923b8d0  Custom  Malloc  1   00:00.389.904   16  Demo    -[AppDelegate applicationDidFinishLaunching:]
1   0x7ff1e923b8d0  Custom  Retain  2   00:00.390.534   0   AppKit  -[NSArrayController _insertObject:atArrangedObjectIndex:objectHandler:]
2   0x7ff1e923b8d0  Custom  Retain  3   00:00.390.554   0   AppKit  -[_NSModelObservingTracker _startObservingModelObject:]
3   0x7ff1e923b8d0  Custom  Retain  4   00:00.390.576   0   AppKit  -[NSArrayController selectedObjects]
4   0x7ff1e923b8d0  Custom  Retain  5   00:00.390.948   0   AppKit  -[NSArrayController removeObject:]
5   0x7ff1e923b8d0  Custom  Release 4   00:00.390.977   0   AppKit  -[_NSModelObservingTracker _stopObservingModelObject:]
6   0x7ff1e923b8d0  Custom  Release 3   00:00.390.984   0   AppKit  -[NSArrayController _removeObjectsAtArrangedObjectIndexes:contentIndexes:objectHandler:]
7   0x7ff1e923b8d0  Custom  Release 2   00:00.391.387   0   Foundation  -[NSNotificationCenter postNotificationName:object:userInfo:]
8   0x7ff1e923b8d0  Custom  Release 1   00:00.395.916   0   Foundation  -[NSAutoreleasePool drain]
9   0x7ff1e923b8d0  Custom  Release 0   00:00.395.919   0   Foundation  -[NSAutoreleasePool drain]
4

1 回答 1

1

-retainCount 没用。使用 Instruments 查看代码中是否存在实际的内存泄漏。

于 2013-02-12T03:57:09.700 回答