views:

26

answers:

1

I have a weird problem in my Objective-C code. I have a View Controller where I call my own loadData method in "viewWillAppear". This works cool until the view becomes visible the third time.

Than the app crashes without any exceptions or other hints when I call

NSArray *storeListArray = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

I checked that the managedObjectContext is not accidentally released somewhere else. But that seems not be the case. The fetchRequest is created right before the call above. I guess it crashes somewhere inside the executeFetchRequest method.

Has anybody an idea where I could look for the error? What is really bugging me is the fact that it reproducible happens on the third call.

+1  A: 

Allright... I solved the problem (I guess). As far as I can see, the problem was the "error" of the call.

I made a stupid mistake by not initializing the NSError object before the call. So here is what works for me now:

NSError *error = nil;
NSArray *storeListArray = [MOC executeFetchRequest:fetchRequest error:&error];

Before it was only

NSError *error;

That's what I found in many examples by the way. But it seems like that was at least a part of the problem.

My whole code looks like this now:

MyAppDelegate *app = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *MOC = [app managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Store" inManagedObjectContext:MOC];
[fetchRequest setEntity:entity];

NSSortDescriptor *streetDescriptor = [[NSSortDescriptor alloc] initWithKey:@"street" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:streetDescriptor, nil];    
[fetchRequest setSortDescriptors:sortDescriptors];

NSError *error = nil;
NSArray *storeListArray = [MOC executeFetchRequest:fetchRequest error:&error];

Maybe that helps someone.

thomas
That shouldn't matter, because `executeFetchRequest:error:` should only write to the pointer you passed, not attempt to use whatever object was already there. Run your app with the Zombies instrument and see what it says when you try to reproduce the crash.
Peter Hosey
Yeah, that's what I thought. By the way, now after changing that code to the one above, it crashes on first hit when I don't initialize the error with nil.Instruments won't let me select the "Zombies" instrument. It inserts an "Allocations" instrument instead!?
thomas
Argh... Just saw the WWDC video about memory analysis. Forget the Zombies -> Allocations thing. ;-)The NSZombies instruments says nothing. The app just crashes.
thomas