我有一个正在处理的程序,这就是发生的事情:
我有一个调用对象的视图控制器(NewGameViewController)。该对象将.txt文件读入数组,然后执行while循环以填充Core Data数据库。我有填充数据库的问题,但最终能够排序。但是我现在在返回NewGameViewController后得到EXC_BAD_ACCESS。
我一直试图追查问题,但没有成功(我是新手)。但是我确实认为问题出在while循环中。我无法弄清楚如何识别错误,甚至无法确定崩溃的确切位置。
顺便说一句,似乎数据库的实际加载效果很好,而且只是记录最后一条.txt记录包含“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];
}
答案 0 :(得分:4)
崩溃发生在哪里?您应该能够从调试器获得回溯,或者应该有可用的崩溃报告。
这是一个问题:
NSString *qNr = [[NSString alloc] init];
... later ....
qNr = [myArray objectAtIndex:xx];
... even later ...
[qNr release];
这是泄漏(没有释放第一个字符串的赋值)和崩溃(释放从未保留的对象)。
还有一个:
NSString *myObject1 = [[NSString alloc] init];
myObject1 = [myArray objectAtIndex:xx];
...
[myObject1 release];
在这两种情况下,alloc/init
和release
都不是必需的。
memory management guide可能会非常有用。
答案 1 :(得分:0)
您正在发布已经发布的内容(可能)。
尝试取出线条 [qNr发布]; [request2 release]; [请求发布];
看看会发生什么。
使用请求命令,您通常可以执行自动释放,以防止泄漏。
答案 2 :(得分:0)
最好使用NSZombie来找出实际问题。通常它会发生,因为你发布了两次相同的内存。
干杯, Raxit