gpt4 book ai didi

cocoa - NSArrayController addObject/removeObject 以不同方式更改引用计数

转载 作者:行者123 更新时间:2023-12-03 17:55:08 27 4
gpt4 key购买 nike

我假设 NSArrayController 的 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]

最佳答案

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

关于cocoa - NSArrayController addObject/removeObject 以不同方式更改引用计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14807595/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com