NSPointerArray weird compaction

1.9k Views Asked by At

I have a weak NSPointerArray with some NSObject that has been released. Before calling compact what I see is:

(lldb) po [currentArray count]
1
(lldb) po [currentArray pointerAtIndex:0]
<nil>
(lldb) po [currentArray allObjects]
<__NSArrayM 0x16f04f00>(

)

That makes sense, but what is really weird is that when I call compact on that array I see the same values! Count still returns 1 and pointerAtIndex:0 is nil.

Why the nil hasn't been removed?

EDIT

Here's the full code (yeah it's XCTesting framework):

- (void)testCompaction {
    __weak id testingPointer = nil;

    NSPointerArray *weakArray = [NSPointerArray weakObjectsPointerArray];

    @autoreleasepool {

        NSObject *someObj = [[NSObject alloc] init];

        testingPointer = someObj;

        [weakArray addPointer:(__bridge void*)testingPointer];

        NSLog(@"before compaction inside autorelease: testingPointer = %@ count = %d, allObjects = %@, pointerAtIndex:0 = %@, pointerAtIndex:0 class = %@", testingPointer, [weakArray count], [weakArray allObjects], [weakArray pointerAtIndex:0], [(id)[weakArray pointerAtIndex:0] class]);

        someObj = nil;
    }

    NSLog(@"before compaction outside autorelease: testingPointer = %@ count = %d, allObjects = %@, pointerAtIndex:0 = %@, pointerAtIndex:0 class = %@", testingPointer, [weakArray count], [weakArray allObjects], [weakArray pointerAtIndex:0], [(id)[weakArray pointerAtIndex:0] class]);

    [weakArray compact];

    NSLog(@"after compaction outside autorelease: testingPointer = %@ count = %d, allObjects = %@, pointerAtIndex:0 = %@, pointerAtIndex:0 class = %@", testingPointer, [weakArray count], [weakArray allObjects], [weakArray pointerAtIndex:0], [(id)[weakArray pointerAtIndex:0] class]);
}

and logs:

  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)   

Why the compact method does not delete the first pointer? It's clearly a nil before calling compact.

3

There are 3 best solutions below

0
On BEST ANSWER

I've seen this same behavior. Here is an open radar bug report: http://www.openradar.me/15396578

0
On

It's already a few year and using the sample code shows no change.

However, -compact is perfectly working when the update of the pointer is performed using the method -replacePointerAtIndex:withPointer:

In the code above, replacing

someObj = nil;

by

 [weakArray replacePointerAtIndex:0 withPointer:nil];

provides the expected result after [weakArray compact]; and the code will crash at the last NSLog

1
On

The reason this happens is that -compact first checks whether an internal flag 'needsCompaction' is set. If it's not, it simply bails early. The only time the flag is set is if a nil pointer is inserted directly into the array through the public interface. It does not get set if a weakly referenced object is deallocated (and the pointer is nil'd) after the pointer was inserted into the array.

One work around for this behavior is to purposefully append a nil pointer to the array before calling -compact. Not ideal but it'll work.

[pa addPointer:nil]; // forces the pointer array to do compaction next time
[pa compact];