7

I'm using a weak objects NSHashTable that is reporting a count of 1 even though it's empty. The following is lldb output displaying my case.

p [__operationWaitList count]
(NSUInteger) $4 = 1

p [__operationWaitList.allObjects count]
(NSUInteger) $7 = 0

My best guess is that count counts the number of weak references stored in the NSHashTable rather than the number of valid objects. By calling allObjects however I'm dereferencing the pointers which uncovers that there are no valid objects, hence the resulting object array is empty.

I'd like confirmation of this since the documentation is a bit lacking on this point.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
greg
  • 1,926
  • 17
  • 26

1 Answers1

12

Experimental results, non-exhaustive testing, suggest:

  • Your observation is correct, count returns the number of references currently in the hash table some of which may be null.

  • Also as you observed allObjects.count always returns the number of non-null references.

  • Adding a new distinct object appears to cleanup null references.

Documentation is not clear on the issue.

Suggest you file a bug report with Apple, they should either fix the implementation or the documentation.

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Thanks for confirmation. – greg Apr 26 '15 at 23:29
  • 1
    Apple responded to the bug and maintains that the behavior is as intended. They have opened a bug for documentation fix though to explain the behavior better. They also indicate that two call to `allObjects` can lead to objects being left in the autorelease pool. – greg May 02 '15 at 01:02