Objective c 非点阵压实
我有一个弱的Objective c 非点阵压实,objective-c,automatic-ref-counting,nspointerarray,Objective C,Automatic Ref Counting,Nspointerarray,我有一个弱的NSPointerArray,其中一些NSObject已经发布。在调用compact之前,我看到的是: (lldb) po [currentArray count] 1 (lldb) po [currentArray pointerAtIndex:0] <nil> (lldb) po [currentArray allObjects] <__NSArrayM 0x16f04f00>( ) 和日志: before compaction inside au
NSPointerArray
,其中一些NSObject
已经发布。在调用compact
之前,我看到的是:
(lldb) po [currentArray count]
1
(lldb) po [currentArray pointerAtIndex:0]
<nil>
(lldb) po [currentArray allObjects]
<__NSArrayM 0x16f04f00>(
)
和日志:
before compaction inside autorelease: testingPointer = <NSObject: 0x7de7ff80> count = 1, allObjects = (
"<NSObject: 0x7de7ff80>"
), pointerAtIndex:0 = <NSObject: 0x7de7ff80>, pointerAtIndex:0 class = NSObject
2015-07-20 14:27:14.062 AppetizeSuite copy[54144:9019054] before compaction outside autorelease: testingPointer = (null) count = 1, allObjects = (
), pointerAtIndex:0 = (null), pointerAtIndex:0 class = (null)
2015-07-20 14:27:22.615 AppetizeSuite copy[54144:9019054] after compaction outside autorelease: testingPointer = (null) count = 1, allObjects = (
), pointerAtIndex:0 = (null), pointerAtIndex:0 class = (null)
压缩内部自动释放之前:testingPointer=count=1,allObjects=(
""
),pointerAtIndex:0=,pointerAtIndex:0类=NSObject
2015-07-20 14:27:14.062开胃套餐拷贝[54144:9019054]在自动释放外压缩之前:testingPointer=(null)count=1,allObjects=(
),pointerAtIndex:0=(null),pointerAtIndex:0 class=(null)
2015-07-20 14:27:22.615开胃套餐拷贝[54144:9019054]在自动释放外压缩后:testingPointer=(null)count=1,allObjects=(
),pointerAtIndex:0=(null),pointerAtIndex:0 class=(null)
为什么
compact
方法不删除第一个指针?在调用compact
之前,它显然是一个nil
,我也看到过同样的行为。以下是一份开放式雷达错误报告:
发生这种情况的原因是
-compact
首先检查是否设置了内部标志“NeedsCompact”。如果不是,它只是提前退出。唯一设置该标志的时间是通过公共接口将nil指针直接插入数组中。如果弱引用对象在指针插入数组后被解除分配(指针为nil'd),则不会设置该值
这种行为的一种解决方法是在调用-compact
之前有目的地向数组追加一个nil指针。不太理想,但会有用的
[pa addPointer:nil]; // forces the pointer array to do compaction next time
[pa compact];
已经过了几年,使用示例代码显示没有变化 但是,-compact在使用-replacePointerAtIndex:withPointer: 在上面的代码中,替换
someObj = nil;
借
提供
[weakArray compact]之后的预期结果
并且代码将在最后一次NSLog时崩溃您是如何将已释放的某个NSObject添加到指针数组的?你在使用weakobjectspinterarray吗?@KazukiSakamoto你能看看我上面更新的问题吗?谢谢你的回答。这对我很有用。
someObj = nil;
[weakArray replacePointerAtIndex:0 withPointer:nil];