iOS: unexplained -[__NSOrderedSetM removeObjectAtIndex:]: index 0 beyond bounds for empty ordered set

125 Views Asked by At

I got -[__NSOrderedSetM removeObjectAtIndex:]: index 0 beyond bounds for empty ordered set in the following code:

- (void)moveAdapter:(SUBaseAdapter *)adapter
               from:(NSMutableOrderedSet *)src
                 to:(NSMutableOrderedSet *)trg {

    SS_ASSERT(adapter);
    SS_ASSERT(src);
    SS_ASSERT(trg);

    dispatch_barrier_sync(self.adaptersQueue, ^{ // CONCURRENT QUEUE

        if (![src containsObject:adapter]) {
            NSString *message = [NSString stringWithFormat:@"moveAdapter: %@ not in src", adapter.providerName];
            [[SULoggerManager sharedInstance] log:message level:SU_LOG_INTERNAL tag:TAG_INTERNAL];
            return;
        }
        if ([trg containsObject:adapter]) {
            NSString *message = [NSString stringWithFormat:@"moveAdapter: %@ already in trg", adapter.providerName];
            [[SULoggerManager sharedInstance] log:message level:SU_LOG_INTERNAL tag:TAG_INTERNAL];
            return;
        }

        [trg addObject:adapter];
        [trg sortUsingDescriptors:@[self.sortDescriptor]];
        [src removeObject:adapter]; //CRASH HERE

    });

}

So, I'm not explicitly calling removeObjectAtIndex:, but maybe removeObject:adapter does?

Here's what I don't understand:

  1. I tried adding anObject to a set, then called removedObject:anObject twice, had no problem.
  2. I thought maybe it's a concurrency issue, where two different threads enter removeObject thinking it's in position 1, and then the second thread fails, but I am using dispatch_barrier_sync, and I don't remove objects from 'src' anywhere else.

Any hints?

0

There are 0 best solutions below