0

I have the following class:

#import "SharedData.h"
static int selectedCountryIndex;
static NSMutableArray *imageDataObjectsArray;
@implementation SharedData
+(void)insertIntoImageDataObjectsArray:(ImageData *)imageData:(int)index{
    if (!imageDataObjectsArray)
        **imageDataObjectsArray = [[NSMutableArray alloc]init ];**

    [imageDataObjectsArray insertObject:imageData atIndex:index];
}
+(ImageData *)getFromImageDataObjectsArray:(int)index{
    return [imageDataObjectsArray objectAtIndex:index];
}
+(void)setSelectedCountryIndex:(int)selectedCountryIndexArg{
    selectedCountryIndex = selectedCountryIndexArg;
}
+(int)getSelectedCountryIndex{
    return selectedCountryIndex;
}
@end

This class is just meant to accept data from one view, and then allow another view to fetch that data. However, whenever the insertIntoImageDataObjectsArray method is called, the line marked with asterisks causes an "EXC_BAD_ACCESS" crash. This is the call to that method:

[SharedData insertIntoImageDataObjectsArray:imageDataObject :[result doubleValue]-1]; 

Anyone have any idea why?

6
  • 3
    Show the error message. That line can't really crash Commented Oct 15, 2012 at 16:13
  • 1
    check out whether your index is negative value.. Commented Oct 15, 2012 at 16:16
  • The error message is in the question: "EXC_BAD_ACCESS". That's all it's giving me. Commented Oct 15, 2012 at 16:28
  • Did you single step through to see where it actually dies? Commented Oct 15, 2012 at 16:45
  • 1
    Don't prefix methods with get. Commented Oct 15, 2012 at 16:47

1 Answer 1

1

I expect it is this line that is crashing:

[imageDataObjectsArray insertObject:imageData atIndex:index];

And I expect it is crashing because you are attempting to insert at an index that is larger than the array.

0 => "value1",
1 => "value2"

[imageDataObjectsArray insertObject:@"value3" atIndex:1]; would succeed and produce

0 => "value1",
1 => "value3",
2 => "value2"

Subsequently calling [imageDataObjectsArray insertObject:@"value4" atIndex:5]; would fail as index 5 > max index (2)

Or as a commenter pointed out, a negative number is also invalid as an index

Sign up to request clarification or add additional context in comments.

5 Comments

I changed [imageDataObjectsArray insertObject:imageData atIndex:index]; to [imageDataObjectsArray addObject:imageData]; and I still get the same problem.
Are you using ARC? It could be that your array was prematurely deallocated.
I am using ARC. How do I make sure that it isn't deallocated prematurely? I figured the whole point of making it static was that it wouldn't be deallocated.
I just noticed that 3 objects are added to the array before it crashes, if that provides any clues.
But an invalid index would not produce a EXC_BAD_ACCESS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.