EXC_BAD_ACCESS Error for no conceivable reason

OK, This is a method from my program that keeps giving the EXC_BAD_ACCESS error and crashing. I indicated the line below. questionsShown is a readwrite property and points to an NSMutableArray that I initialize with a capacity of 99 at an earlier point in the program. When I debug everything appears normal in terms of the property being allocated. I assumed there must be some issue with memory management but I am having serious trouble finding the problem. Thanks in advance for any help.

@synthesize questionList;
@synthesize questionLabel;
@synthesize questionsShown;

int numElements = [questionList count];
int r;
if (myCount == numElements){
    [questionLabel setText:@"You have seen all the questions, click next again to continue anyways."];
    [questionsShown release];
    questionsShown = [[NSMutableArray alloc] initWithCapacity:99];
    myCount = 0;
else {
    do {
        r = rand() % numElements;
    } while ([questionsShown indexOfObjectIdenticalTo:r] != NSNotFound);
    NSString *myString = [questionList objectAtIndex:(NSUInteger)r];
    [questionLabel setText:myString];
    [questionsShown addObject:r]; //results in crash with message EXC_BAD_ACCESS


The EXC_BAD_ACCESS is coming from dereferencing r, which is just an integer. Your compiler should be giving you a warning (make pointer from integer without a cast) on that line.

If questionsShown is supposed to be some kind of index set for you (which it appears to be), you might want to either use that class, or you will have to box your integer in an NSNumber object. So:

[questionsShown addObject:[NSNumber numberWithInt:r]];

and when you read it:

[questionsShown indexOfObjectIdenticalTo:[NSNumber numberWithInt:r]]

I recommend, however, that you take a look at the NSIndexSet documentation.

With a mutable index set, you could do:

[questionsShownIndexSet containsIndex:r]


[questionsShownIndexSet addIndex:r]

Need Your Help

Excel Upload to database table

asp.net ms-office etl

I'm looking for the best solution to allow our users to upload XLS spreadsheet so that they can be used to populate tables in our data warehouse (DW).

Data frame column value with range condition

arrays r matrix dataframe

I want to create the columns below wherein I can specify the range to apply the value 1 to one column and the rest 0. I know this has been asked and answered perfectly here before but I can't find ...