PeterK
PeterK

Reputation: 4301

EXC_BAD_ACCESS and i do not know how to find the problem

I have a program that i am working on and this is what happen:

I have a view controller (NewGameViewController) that calls an object. That object reads a .txt file into an array and then do a while loop to populate a Core Data database. I have had problems to populate the database but finally was able to sort that out. However i now get EXC_BAD_ACCESS after return to NewGameViewController.

I have been trying to track down the problem but have not been successful (i am new to this). However i do believe the problem is somewhere in the while loop. I just cannot figure out how to identify the error, or even exactly where it crash.

BTW, it seems like the actual loading of the database works well and just for the record the last .txt record consists of "99999".

- (void)populateTheDatabase {
NSLog(@"\n \n >>>>> START populateTheDatabase <<<<< \n \n");
NSLog(@">>populateTheDatabase<<");
//
// Init parameters
NSError *error = nil;
int xx = 0;         // Loop number
int eOF;            // Keep track of EOF in array
int keyNumber = 0;
NSString *qNr = [[NSString alloc] init];
int myInt;
//
//=========PREPARE CORE DATA DB===========//
if (managedObjectContext == nil) { managedObjectContext = [(FamQuiz_v2AppDelegate *)
                                                           [[UIApplication sharedApplication] delegate] managedObjectContext]; }
// Define qContext
NSManagedObjectContext *qContext = [self managedObjectContext];
//===============DELETE ALL RECORDS================//
NSLog(@"\n \n Delete all records");
NSFetchRequest * allQzs = [[NSFetchRequest alloc] init];
[allQzs setEntity:[NSEntityDescription entityForName:@"questions" inManagedObjectContext:qContext]];
[allQzs setIncludesPropertyValues:NO]; //only fetch the managedObjectID
//
error = nil;
NSArray * qZs = [qContext executeFetchRequest:allQzs error:&error];
[allQzs release];
//error handling goes here
for (NSManagedObject * rZ in qZs) {
    [qContext deleteObject:rZ];
}
//
//=========COUNT ENTITIES IN DB BEFORE INSERT DATA===========
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity: [NSEntityDescription entityForName: @"questions" inManagedObjectContext: qContext]];
//
error = nil;
NSUInteger countE = [qContext countForFetchRequest: request error: &error];
NSLog(@"#entities in DB before insert: %i", countE);
[request release];
//
//
// Fill myString with questions from the .txt file and then read .txt file
NSString *filePath = @"/Users/PeterK/Documents/FamilyQuiz/FamQuiz_v2_02/loadq101023v2.txt";
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];
//
// Load array
NSArray* myArray = [myString componentsSeparatedByString:@"\r"];
//NSLog (@"\n \n Number of elements in myArrayX = %i", [myArrayX count]);
NSLog (@"\n \n Number of elements in myArray = %i", [myArray count]);
//
//NSLog(@"myArray: %@", myArray);
//
while (eOF != 99999) {
    NSLog(@"xx: %i", xx);
    qNr = [myArray objectAtIndex:xx];
    eOF = [qNr intValue];
    //
    if (eOF != 99999) {
        NSManagedObject *famQuizInfo = [NSEntityDescription
                                        insertNewObjectForEntityForName:@"questions" 
                                        inManagedObjectContext:qContext];
        // index = 0 : 'record number'
        [famQuizInfo setValue:[NSNumber numberWithInt:keyNumber] forKey:@"idQ"];
        keyNumber = keyNumber++;
        NSLog(@"nr: %i - myArray 0: %@", xx, [myArray objectAtIndex:xx]);
        xx = xx++;
        // index = 1 : 'difficulties level'
        NSString *myObject1 = [[NSString alloc] init];
        myObject1 = [myArray objectAtIndex:xx];
        myInt = [myObject1 intValue];
        [famQuizInfo setValue:[NSNumber numberWithInt:myInt] forKey:@"qDiff"];
        NSLog(@"nr: %i - myArray 1: %@", xx, [myArray objectAtIndex:xx]);
        xx = xx++;
        [myObject1 release];
        // index = 2 : 'category' not existing in the DB anymore, but exist in the .txt
        NSLog(@"nr: %i - myArray 2: %@", xx, [myArray objectAtIndex:xx]);
        xx = xx++;
        // index = 3 : 'question'
        [famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"question"];
        NSLog(@"nr: %i - myArray 3: %@", xx, [myArray objectAtIndex:xx]);
        xx = xx++;
        // index = 4 : 'right answer'
        [famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"qRightAnswer"];
        NSLog(@"nr: %i - myArray 4: %@", xx, [myArray objectAtIndex:xx]);
        xx = xx++;
        // index = 5 : 'wrong answer #1'
        [famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"qWrongAnswer1"];
        NSLog(@"nr: %i - myArray 5: %@", xx, [myArray objectAtIndex:xx]);
        xx = xx++;
        // index = 6 : 'wrong answer #2'
        [famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"qWrongAnswer2"];
        NSLog(@"nr: %i - myArray 6: %@", xx, [myArray objectAtIndex:xx]);
        xx = xx++;
        // placeholder for number of times the question is asked
        [famQuizInfo setValue:[NSNumber numberWithInt:0] forKey:@"qNrAccess"];
        // date when the question was registred
        [famQuizInfo setValue:[NSDate date] forKey:@"qRegDate"];
        //
        NSLog (@"\n ============== \n");
        //
        //
        if (![qContext save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
            error = nil;
            if (![qContext save:&error]) {
                NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
                //
                if(![[famQuizInfo managedObjectContext] save:&error]) {
                    NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
                    NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
                    if(detailedErrors != nil && [detailedErrors count] > 0) {
                        for(NSError* detailedError in detailedErrors) {
                            NSLog(@"  DetailedError: %@", [detailedError userInfo]);
                        }
                    }
                    else {
                        NSLog(@"  %@", [error userInfo]);
                    }
                }
            }
        }
    }
}
//
//
//=========COUNT ENTITIES IN DB AFTER INSERT DATA===========
NSFetchRequest *request2 = [[NSFetchRequest alloc] init];
[request2 setEntity: [NSEntityDescription entityForName: @"questions" inManagedObjectContext: qContext]];

error = nil;
NSUInteger count2 = [qContext countForFetchRequest: request2 error: &error];
NSLog(@"#entities in DB after insert: %i", count2);
[request2 release];
//
//
[qNr release];

}

Upvotes: 0

Views: 1144

Answers (3)

Raxit
Raxit

Reputation: 824

Better you use the NSZombie to find out the actual problem. generally it happens because you are releasing same memory twice.

Cheers, Raxit

Upvotes: 0

bbum
bbum

Reputation: 162712

If there is a crash, there is a backtrace. Post it.

Where is the crash happening? You should be able to get a backtrace from the debugger or there should be a crash report available.


Here is a problem:

 NSString *qNr = [[NSString alloc] init];
 ... later ....
 qNr = [myArray objectAtIndex:xx];
 ... even later ...
 [qNr release];

That is a leak (the assignment without releasing the first string) and a crash (releasing an object that was never retained).

And another one:

    NSString *myObject1 = [[NSString alloc] init];
    myObject1 = [myArray objectAtIndex:xx];
    ...
    [myObject1 release];

In both cases, neither the alloc/init, nor the release are necessary.


The memory management guide will likely be quite helpful.

Upvotes: 4

Walter
Walter

Reputation: 5887

You are releasing something that is already released (probably).

Try taking out the lines [qNr release]; [request2 release]; [request release];

and see what happens.

With request commands you can usually do an autorelease that will keep you from leaking.

Upvotes: 0

Related Questions