0

I have a mutable array property declared and synthesized:

@property (nonatomic, strong) NSMutableArray *arrayOfTasks;

I am using KVC collection Accessors for the same property and also I have other methods which will internally call this KVC Collection accessor method like this:

-(void)insertObject:(CSTaskAbstract *)inTask inArrayOfTasksAtIndex:(NSUInteger)index
{
        [[self arrayOfTasks] insertObject:inTask
                                  atIndex:index];
}
-(void)addObjectInArrayOfTasks:(CSTaskAbstract *)inTask
{
        [self insertObject:inTask
     inArrayOfTasksAtIndex:[[self arrayOfTasks] count]];
}

I had to do some modifications and add the object into the array only when a particular condition is satisfied, so to make sure that this check goes into the designated method, I included the following in the -insertObject KVC Collection accessor method:

-(void)insertObject:(CSTaskAbstract *)inTask inArrayOfTasksAtIndex:(NSUInteger)index
{
    if ([inTask isOperatable])
    {
        [[self arrayOfTasks] insertObject:inTask
                                  atIndex:index];
    }
}
-(void)addObjectInArrayOfTasks:(CSTaskAbstract *)inTask
{
        [self insertObject:inTask
     inArrayOfTasksAtIndex:[[self arrayOfTasks] count]];
}

Now each time when I trigger -addObjectINArrayOfTasks method and if the -isOperatable condition returns boolean NO, the app crashes with no stack trace at all! (Stack trace is at main() of the application). All it says is "index 0 beyond bounds for empty array error".

I am not understanding the reason for this, I am not trying to access the array yet, so I am not giving a chance for framework to complain me that there is no element at index 0. Moreover, I am doing the count of array items check everywhere before accessing the objects out of array. For, if I was trying to access and element out of the bounds index, the app would crash at the same point and let me know exactly where I was trying to access the index out of bounds. That would have been a simple straightforward fix.

Now, to just cross verify, I made a small change in the code like this, which seems to work:

-(void)insertObject:(CSTaskAbstract *)inTask inArrayOfTasksAtIndex:(NSUInteger)index
{
        [[self arrayOfTasks] insertObject:inTask
                                  atIndex:index];
}
-(void)addObjectInArrayOfTasks:(CSTaskAbstract *)inTask
{
    if ([inTask isOperatable])
    {
        [self insertObject:inTask
     inArrayOfTasksAtIndex:[[self arrayOfTasks] count]];
    }
}

I can go ahead with this approach which is working and does not crash, but my concerns are the following:

  1. Adding the same check in designated method would be an added advantage in future if some other programmer would want to invoke the designated method from somewhere else.

  2. Why would the app crash in first case when I wont insert the object into the array in KVC collection accessors based on some condition check?

Thanks for any inputs,

Raj

Raj Pawan Gumdal
  • 7,390
  • 10
  • 60
  • 92

1 Answers1

2

I think the crash you are seeing is more likely related to internal KVC behavior than your array. That might be the reason you don't see a usable stack trace. Have you enabled the exception breakpoint in Xcode?

KVC basically expects that -insertObject:in<Key>AtIndex: will insert a new object at the given index (presumably 0 in your case). Since it assumes that the object was inserted it should now be accessible by queuing the data structure (NSMutableArray) for the object at the given index. When the condition evolves to NO, you fail to insert this object, which means that an index out of bounds exception is possible when KVO tries to query using the provided index.

The second code snipped you posted avoids this error by not calling the KVC collection accessor when an insertion is not needed.

If you want to minimize the chance of someone incorrectly using those methods, expose just -addObjectInArrayOfTasks: in your public header. In addition you can document this. If you want to make it absolutely certain that -insertObject:in<Key>AtIndex: can't be accessed on int's own, you can add an NSAssert, that checks if the method was called from -addObjectInArrayOfTasks:.

Matej Bukovinski
  • 6,152
  • 1
  • 36
  • 36
  • Yes, I think what you told about KVC makes sense. Am curious about how to assert based on the caller of the method which you mentioned, could you please explain it to me? – Raj Pawan Gumdal Nov 09 '12 at 10:25
  • Well, one way of doing this could be to define a BOOL instance variable and set it to YES inside `-addObjectInArrayOfTasks:` before calling `-insertObject:inAtIndex:`. In the later you would than use the assertion to check if the value is set and than reset it back to NO. If you are calling this from multiple threads you would also need some sort of synchronization for the code segment between the flag being set and reset. – Matej Bukovinski Nov 09 '12 at 10:49
  • Ok understood, yes this is possible. Accepting your answer as am convinced with your thought process :) – Raj Pawan Gumdal Nov 09 '12 at 19:57