views:

3063

answers:

6

Hi, I'm currently writing an Iphone application using Core Data and I get a EXC_BAD_ACCESS error during the [managedObjectContext save:&&error] code line. This crash only happens after I modify certain fields. More specifically my entity has two string fields (out of about 10 fields), that get their values from a the return of a modal view controller (like a text editor). The crash also only happens after these fields are edited, the first time I put a value in it works fine.

The reason I have string with format constructors with just strings is because I was trying to copy construct... not sure if that happens automatically? Thought maybe retain/release messages from those strings (those two are from the modal view controller), were getting released on dismissal of the modal view controller or something. Guess not though because it still doesn't work.

Here's the code section that is crashing:

[EDITED]

        - (void)actionSheet:(UIActionSheet *)modalView clickedButtonAtIndex:    (NSInteger)buttonIndex
      switch(buttonIndex) {
           case 0: {
          if(message == nil) {
         message = [NSEntityDescription insertNewObjectForEntityForName:@"MailMessage" inManagedObjectContext:self.managedObjectContext];
           }
           message.toString = txtTo.text;
          message.fromString = txtFrom.text;
          message.subjectString = txtSubject.text;
          message.backgroundColor = [NSNumber numberWithInt:[bgColor intValue]];
           message.textArray = [NSString stringWithFormat:@"%@", stringTextArray];
          message.htmlString = [NSString stringWithFormat:@"%@", stringHTML];
           message.timeStamp = [NSDate date];
          message.statusCode = [NSNumber numberWithInt:0];
          NSError *error = nil;
          if (![message.managedObjectContext save:&error]) {
             abort();
          } 
          break;
            }
           case 1: {
       break;
           }
      }
      if(buttonIndex != modalView.cancelButtonIndex) {
   [webViewBody loadHTMLString:@"<html><head></head><body></body></html>" baseURL:[NSURL URLWithString:@""]];
   [self.navigationController popToRootViewControllerAnimated:YES];
}

}

And here's the crash log:

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x00000015
Crashed Thread:  0

Thread 0 Crashed:
0   libobjc.A.dylib                 0x30011940 objc_msgSend + 20
1   CoreData                        0x367f7d3e -[NSKnownKeysDictionary1 dealloc] + 82
2   CoreData                        0x367f7cda -[NSKnownKeysDictionary1 release] + 34
3   CoreData                        0x3687eec4 -[NSManagedObject(_NSInternalMethods) _setOriginalSnapshot__:] + 40
4   CoreData                        0x36821030 -[NSManagedObjectContext(_NSInternalAdditions) _clearOriginalSnapshotAndInitializeRec:] + 16
5   CoreData                        0x368205f2 -[NSManagedObjectContext(_NSInternalAdditions) _didSaveChanges] + 958
6   CoreData                        0x368133bc -[NSManagedObjectContext save:] + 412
7   Decome                          0x0001fdd6 -[CreateMessageViewController actionSheet:clickedButtonAtIndex:] (CreateMessageViewController.m:163)
8   UIKit                           0x30a6cbd8 -[UIActionSheet(Private) _buttonClicked:] + 256
9   CoreFoundation                  0x30256dd4 -[NSObject performSelector:withObject:withObject:] + 20
10  UIKit                           0x3096e0d0 -[UIApplication sendAction:to:from:forEvent:] + 128
11  UIKit                           0x3096e038 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
12  UIKit                           0x3096e000 -[UIControl sendAction:to:forEvent:] + 44
13  UIKit                           0x3096dc58 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 528
14  UIKit                           0x309a6e9c -[UIControl touchesEnded:withEvent:] + 452
15  UIKit                           0x309a60d4 -[UIWindow _sendTouchesForEvent:] + 520
16  UIKit                           0x309a5464 -[UIWindow sendEvent:] + 108
17  UIKit                           0x30936e3c -[UIApplication sendEvent:] + 400

Any help is appreciated, Thanks.

UPDATE: Also, even though the program crashes, when I open it back up it the data has saved correctly. So the EXC_BAD_ACCESS must happen after the save has gotten at least far enough to store in the persistent store i think.

If I comment out the save line, the code runs fine now. But it doesn't save after i close and exit. If I run the save line in my Root View Controllers willAppear function, it throws the same EXC_BAD_ACCESS error. The console doesn't say anything other than EXC_BAD_ACCESS if I do a backtrace I get :

#0  0x30011940 in objc_msgSend ()
#1  0x367f7d44 in -[NSKnownKeysDictionary1 dealloc] ()
#2  0x367f7ce0 in -[NSKnownKeysDictionary1 release] ()
#3  0x3687eeca in -[NSManagedObject(_NSInternalMethods) _setOriginalSnapshot__:] ()
#4  0x36821036 in -[NSManagedObjectContext(_NSInternalAdditions) _clearOriginalSnapshotAndInitializeRec:] ()
#5  0x368205f8 in -[NSManagedObjectContext(_NSInternalAdditions) _didSaveChanges] ()
#6  0x368133c2 in -[NSManagedObjectContext save:] ()
#7  0x0000314e in -[RootViewController viewWillAppear:] (self=0x11b560, _cmd=0x3014ecac, animated=1 '\001') at /Users/inckbmj/Desktop/iphone/Decome/Classes/RootViewController.m:85

Sorry the code wasn't properly formatted before. When this view controller gets created if it is not a new "message" it is passed a message object obtained from a fetchedResultsController like so:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MailMessage *aMessage = (MailMessage *)[fetchedResultsController objectAtIndexPath:indexPath];
    [messageView loadMessage:aMessage viewOnly:NO usingTemplate:NO];
    messageView.managedObjectContext = self.managedObjectContext;
    [self.navigationController pushViewController:messageView animated:YES]; 
}

(the first set of code is from the MessageViewController.m file which is the class that messsageView is)

It only crashes if I present my EditorViewController as a modal view and then return. Even if I change the textArray and htmlString lines (which are the only things the modal view affects) to:

message.textArray = @"HELLO";
message.htmlString = @"HELLO";

it still crashes. If I comment both lines out however it doesn't crash.

So it seems like it crashes if I present a modal view and then try to edit either the textArray or htmlString fields of my NSOManagedObject...

Here is where i present the view:

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
    if(!viewOnly) {
     UITouch *touch = [touches anyObject];
     CGPoint location = [touch locationInView: txtTo];
 location = [touch locationInView: webViewBody];
     if(CGRectContainsPoint(webViewBody.bounds, location)) {
      [editor loadTextArrayString:stringTextArray];
      [self presentModalViewController:editor animated:YES];
     }
    }
}

and where i dismiss it:

-(void)returnWithTextArray:(NSString *)arrayString HTML:(NSString *)html bgColor:(NSNumber *)numColor {
    [self dismissModalViewControllerAnimated:YES];
    self.stringTextArray = [NSString stringWithFormat:@"%@", arrayString];
    self.stringHTML = [NSString stringWithFormat:@"%@", html];
    self.bgColor = [NSNumber numberWithInt:[numColor intValue]];
    [webViewBody loadHTMLString:self.stringHTML baseURL:[NSURL URLWithString:@""]];
}
A: 

Hello,

Can you please do the following just to eliminate one issue.

  1. Rename your database file that is created eg somethingv1.sqlite
  2. Clean and build.

Does the crash still occur?

John Ballinger
Yeah, the crash still occurs.
kiyoshi
A: 

I have seen many funny behaviours with CoreData on iPhone that were solved by resetting content and settings in the iPhone simulator.

Michael Sharek
Running it on an ipod touch though, but yeah tried resetting content to no avail :(.
kiyoshi
+3  A: 

Solved the problem though I'm not sure I'm addressing the actual root cause. The error was eliminated when I added this line:

[managedObjectContext setRetainsRegisteredObjects:YES];

To where I create the managedObjectContext. So I guess it had to do with retain counts. I'm guessing that maybe instance variables get released partially or temporarily or something when modal views are presented? I don't know. In any case, this error was eliminated the the program works fine now.

kiyoshi
I'm having a similar problem - also fixed with the directive you've included here. What I don't know is ... are we expected now to manually release every entity managed by this moc?
Luther Baker
A: 

I know this is an oldie but I had the same issue so thought I'd add my tuppence as to how I solved the issue, mine was caused by manually releasing the managed object within the modal view, I removed the release calls and everything is working fine :) according to the docs you shouldn't manually try and release managed objects anyway as the context will look after all of that. That's my experience anyway, search through your code for over-released values.

Steff
A: 

Set the FRC cashe to nil

Greg
A: 

Just to help others having the same issue, and reinforcing Steff's response above, the likely cause of this error is that you are trying to release a NSManagedObject.

Rog