EXC_BAD_ACCESS Error for any reason

OK, this is a method from my program that continues to throw an EXC_BAD_ACCESS error and a failure. I have indicated the line below. questionsShown is a readwrite property and points to an NSMutableArray, which I initialize with a throughput of 99 at an earlier point in the program. When I debug, everything looks fine in terms of the allocated property. I suggested that there should be a problem with memory management, but I am having serious problems finding a problem. Thanks in advance for any help.

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

-(IBAction)next{
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];
    myCount++;
    [questionsShown addObject:r]; //results in crash with message EXC_BAD_ACCESS
    myCount++;
}
}
+3
source share
1 answer

EXC_BAD_ACCESS r, . ( ) .

Shown - (, -, ), , NSNumber. :

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

:

[questionsShown indexOfObjectIdenticalTo:[NSNumber numberWithInt:r]]

, , NSIndexSet.

:

[questionsShownIndexSet containsIndex:r]

[questionsShownIndexSet addIndex:r]
+10

Source: https://habr.com/ru/post/1752747/


All Articles