Somewhere:
if([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *email_vc = [[MFMailComposeViewController alloc] init];
email_vc.mailComposeDelegate = self;
[email_vc setSubject:subject];
[email_vc setMessageBody:message isHTML:FALSE];
[email_vc setToRecipients:recipients];
[self presentModalViewController:email_vc animated:FALSE];
[[UIApplication sharedApplication] setStatusBarHidden:TRUE];
[email_vc release];
}
else
...
Somewhere else:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Saved");
break;
case MFMailComposeResultSent:
NSLog(#"Sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Compose result failed");
break;
default:
NSLog(#"Default: Cancelled");
break;
}
// This ugly thing is required because dismissModalViewControllerAnimated causes a crash
// if called right away when "Cancel" is touched.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_current_queue(), ^
{
[self dismissModalViewControllerAnimated:FALSE];
});
}
That ugly "dispatch_after" block is the only way I can get this to work without a crash.
The context is that touching anything other than "Send" on the email compose view controller will cause a crash. Is there a way to deal with this without having to resort to this ugly band-aid? My theory on the band-aid is that an intermediate view is being presented when you touch "Cancel" to confirm that the user really wants to cancel. I am wondering if [self dismissModalViewControllerAnimated:FALSE]; is trying to dismiss a view out of sequence or something to that effect. By inserting a small delay I am theorizing that the mail compose view has time to cleanup before it is asked to go away.
I've seen a delay used in another question. The author did not go into any details though:
Crash On MFMailComposeViewController For iPad
EDIT 1: Adding crash log
Incident Identifier: ****************
CrashReporter Key: *****************
Hardware Model: iPhone4,1
Process: ************* [9038]
Path: /var/mobile/Applications/*********************
Identifier: ***********************
Version: ??? (???)
Code Type: ARM (Native)
Parent Process: launchd [1]
Date/Time: 2012-07-20 11:25:53.704 -0700
OS Version: iPhone OS 5.0.1 (9A405)
Report Version: 104
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0xa003853a
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libobjc.A.dylib 0x316b9fbc 0x316b6000 + 16316
1 UIKit 0x350caa9e 0x34f8e000 + 1297054
2 UIKit 0x34fa6814 0x34f8e000 + 100372
3 UIKit 0x34fabfb2 0x34f8e000 + 122802
4 QuartzCore 0x33354ba0 0x33329000 + 179104
5 libdispatch.dylib 0x37896f74 0x37894000 + 12148
6 CoreFoundation 0x37bac2d6 0x37b20000 + 574166
7 CoreFoundation 0x37b2f4d6 0x37b20000 + 62678
8 CoreFoundation 0x37b2f39e 0x37b20000 + 62366
9 GraphicsServices 0x376adfc6 0x376aa000 + 16326
10 UIKit 0x34fbf73c 0x34f8e000 + 202556
11 ***************** 0x00002346 main (main.m:14)
12 ***************** 0x00002304 start + 32
EDIT 2: After much head scratching it appears that this is a genuine Apple bug.
I downloaded and ran the MailComposer sample project:
http://developer.apple.com/library/ios/#samplecode/MailComposer/Introduction/Intro.html
It works fine.
Then I edited the code to remove the animation while presenting and dismissing the mail composition controller.
[self presentModalViewController:picker animated:FALSE];
and
[self dismissModalViewControllerAnimated:FALSE];
Sure-enough, it crashed when "Cancel" was used to dismiss the email composition UI.
Running zombie brought this out:
-[MFMailComposeController actionSheet:didDismissWithButtonIndex:]: message sent to deallocated instance 0x7479ef0
I guess the action sheet gets the dismiss message instead of the mail compose view controller.
If someone could confirm behavior I'll report the bug.
EDIT 3: Bug reported.
The answer I accepted has a good explanation of the potential mechanism that is causing this issue. Also, during the back and forth in the answer comments two additional work-arounds were identified. All band-aids but now there are a few choices.
I haven't checked yet, but I suspect that ShareKit is subject to this bug as well (if the presentation of the mail compose view controller is not animated).
I guess the action sheet gets the dismiss message instead of the mail compose view controller.
Not quite.
The sequence of events probably happens like this:
Action sheet calls -actionSheet:clickedButtonAtIndex: on its delegate (the MFMCVC).
MFMailComposeViewController calls -mailComposeController:didFinishWithResult:error: on its delegate (your VC)
Your VC calls [self dismissModalViewControllerAnimated:NO]
This causes the MFMCVC to be released. Since the dismiss isn't animated, there is no longer anything referring to the MFMCVC. It gets dealloced!
Action sheet calls -actionSheet:didDismissWithButtonIndex: on its delegate
But its delegate has been dealloced!
So it crashes!
The fix would be for Apple to do actionSheet.delegate = nil in -dealloc.
A potential workaround
[[self.modalViewController retain] autorelease]
[self dismissModalViewControllerAnimated:NO]
This is a bit trickier to do if you are using ARC.
this works for me:
- (void) mailComposeController: (MFMailComposeViewController *) controller
didFinishWithResult: (MFMailComposeResult) result
error: (NSError *) error {
if(result == MFMailComposeResultSent){
[self dismissViewControllerAnimated:YES completion:NULL];
} else if (result == MFMailComposeResultCancelled) {
[self dismissViewControllerAnimated:YES completion:NULL];
}
}
Related
I am developing a simple game on Xcode 4.4 for iOS 5.1 using storyboards, ARC, and a navigation controller. The app works perfectly on the simulator, but not on the device (iPhone 4 CDMA). So basically, I have a main menu with 3 UIButtons (Play Game, Options, Help). When I click on Play Game and then try to go back to the menu via the navigation controller back button, the app crashes on the device. It is stopped at the following thread:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x70000008)
and pointed to the following:
0x35b4df78: ldr r3, [r4, #8]
There is also a point in my code where I am calling the popToRootViewContoller method. It also crashes here (with same thread error as I would've thought). However, if I comment out the viewWillDisappear method, then I am able to switch back and forth with no issue. The Options and Help screen do not implement the viewWillDisappear method and switch back and forth perfectly on the device.
I have the following under the viewWillDisappear method:
-(void)viewWillDisappear:(BOOL)animated
{
[tmrCountdown invalidate];
[tmrEclapsedTime invalidate];
[tmrMainEnemyMovement invalidate];
[tmrMoveSpawnedEnemies invalidate];
[tmrSpawnEnemies invalidate];
accInc=currPrefs.accelerometerSensitivity;
enemySpeedX=5.0;
enemySpeedY=5.0;
countdown=4;
ecMiliseconds=0;
randTime=0;
stopped=NO;
gameStarted=NO;
}
I call the popToRoot method here:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)//cancel
{
//called here
[self.navigationController popToRootViewControllerAnimated:YES];
}
else //1 (Play Again)
{
[self reInit];
}
}
Thanks,
Mehul
As I see from code, you are invalidating timers, and this may occur if you try to invalidate an invalid( a non repeating timer
repeats:NO
or released timer. It is not an exception, so you cant catch it with #try block. It is a signal. What you have to do is:
In your timer selector you need to call
[timer release]; // if you have allocated it
timer=nil;
...
Some action
And in your viewWillDisappear
If (timer!=nil) {
[timer invalidate];
timer=nil;
}
Scenario is like this--
In my app there is an Scroll View with many instances of
MyCustomImageDownloaderController(containing imageViews where images are to be assigned after downloading) .
As per our requirement, an image has to be downloaded as we move on to a page.
Scroll View + (MyCustomImageDownloaderController1, MyCustomImageDownloaderController2, MyCustomImageDownloaderController3.... etc)
Let's say i am scrolling on it,
i reached to page 1 --> image for it should start downloading
i reached to page 2 --> image for it should start downloading...so on
and if i am on page 3 and images for previous pages if not been dowloaded, they should stop downloading.
So i tried it with using threads..
on API..
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender{
Step 1) calculated currentPageNumber
Step 2) started thread for downloading image with url for this currentPage
//startBackGroundThreadForPlaceImage:(NSURL *) url
Step 3)stopped thread for previous page , if that is still running
}
Now My MyCustomImageDownloaderController is as
-(void) startBackGroundThreadForPlaceImage:(NSURL *) url{
if(isImageDownloaded == NO){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//[self performSelectorInBackground:#selector(loadImageInBackground:) withObject:imageUrl];
myThread = [[NSThread alloc] initWithTarget:self selector:#selector(loadImageInBackground:) object:imageUrl];
//[NSThread detachNewThreadSelector:#selector(loadImageInBackground:) toTarget:self withObject:imageUrl];
[myThread start];
NSLog(#"The current thread is %# ", [[NSThread currentThread] name]);
[pool release];
}
}
NOW Here selector does the work of loading image and assigning to image view
Now Stopping the thread
-(void) stopBackgroundThread{
[myThread cancel];
//[[NSThread currentThread] cancel];
//if([[NSThread currentThread] isCancelled]) {
//[NSThread exit];
//}
[NSThread exit];
}
-(BOOL) isThreadRunning{
return [myThread isExecuting];
}
So i tried a lot of things, but could not Stop the thread in between..
Basically once instantiated thread using any of three methods
1) perform Selector in BackGround
2) NSThread detach new thread
3) NSThread alloc..init with..
In first 2 methods how to get the instance of the newly created thread, so that i could stoop it,
as NSThread currentThread doest not give that
in Method 3,
myThread = [[NSThread alloc] initWithTarget:self selector:#selector(loadImageInBackground:) object:imageUrl];
when i tried
[myThread cancel];
It did not cancel that thread,,
When i tried
[NSThread exit];
it hangs on current screen,,,,i guess it has stopped the main thread
Please help me
Thanks in Advance*strong text*
It's generally better to ask the thread to stop, rather than forcing it, which should be considered a last resort. You should, therefore, frequently check a 'stop flag' and when this gets set, terminate the current thread (by simply exiting the method, in this case). You just need to provide a property on the class the thread is operating on so callers can ask the thread to stop.
It's no different in C++ or Java.
Ok, I've spend like half day on this and it's killing me.
So I've got 3 view controllers transitioning from one another, something like this:
I call the UploadDecisionViewController after destroying the previous View Controller:
[self dismissModalViewControllerAnimated:YES];
[self performSelector:#selector(showUDModalView) withObject:nil afterDelay:0.5];
In my showUDModalView method:
- (void)showUDModalView
{
UploadDecisionViewController *udcontroller = [[UploadDecisionViewController alloc] initWithNibName:#"UploadDecisionViewController" bundle:nil];
udcontroller.delegate = self;
[self presentModalViewController:udcontroller animated:YES];
[udcontroller release];
}
The UploadDecisionViewController shows up no problem. The UploadDecisionViewController has a button, which when clicked I want it to transition to the FileUploadViewController. I setup a UploadDecisionDelegate, threw a method in there to handle the button clicking:
Inside UploadDecisionDelegate protocol (UploadDecisionViewController.h):
#protocol UploadDecisionDelegate
//let UOnliveViewController know that a button was selected
- (void)UploadDecisionViewController:(UploadDecisionViewController *)controller madeChoice:(NSString *)whichDirection;
#end
Then inside my IBAction method when the button is clicked, I have this:
- (IBAction)decisionSelected:(id)sender
{
[delegate UploadDecisionViewController:self madeChoice:#"upload"];//crashing at this line
}
When I run this, at this line above it is throwing a runtime exception:
2010-06-09 12:48:59.561 UOnlive[4735:207] *** -[UIView UploadDecisionViewController:madeChoice:]: unrecognized selector sent to instance 0x3b65420
2010-06-09 12:48:59.562 UOnlive[4735:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIView UploadDecisionViewController:madeChoice:]: unrecognized selector sent to instance 0x3b65420'
2010-06-09 12:48:59.563 UOnlive[4735:207] Stack: (
33502299,
2495698185,
33884219,
33453686,
33306306,
20618,
2982917,
3390286,
3399023,
3394235,
3087839,
2996168,
3022945,
40156505,
33287040,
33283144,
40150549,
40150746,
3026863,
11700,
11554
)
Let me throw in the delegate method implemented also:
- (void)UploadDecisionViewController:(UploadDecisionViewController *)controller madeChoice:(NSString *)whichDirection
{
NSLog(#"it got to here 245");
[self dismissModalViewControllerAnimated:YES];
if (yesOrNo) {
//open up the FileUploadViewController and proceed to upload
[self performSelector:#selector(showFUModalView) withObject:nil afterDelay:0.5];
}
}
Can someone tell me what the heck is going on? Thanks a bunch for the help...
The error says that you are trying to call the UploadDecisionViewController method on UIView.
My bet is that you set some view to the delegate instead of view controller.
Where the showUDModalView method is located?
Maybe you set the delegate in some additional places?
Your code is to lengthy and I dont want to go through this but just an advice, you can c if your object can/cannot perform a selector with this kind of statement:
if([myObj respondsToSelector:#selector(myFunc)])
{
//do something
}
else{
//do something else
}
A method is called when a return button on the keyboard is pressed. After calling another method which returns an integer a message is created based on that integer. The message is then passed into an UIAlterView and displayed to the user. The alert doesn't have any options (hence why I'm not calling a delegate), but simply notifies the user of what happened.
Edit: Below is the full method (previously displayed partial). When I comment out everything before the UIAlertView and substitute the string #"test" instead of passing message the Alert is shown successfully. Am I not handling memory correctly with my structure?
- (IBAction)joinButton {
struct userInfo localUser;
[emailAddress resignFirstResponder];
//convert textField text to char array in structure
localUser.firstName = [self convertStringtoCharArray:firstName.text];
localUser.lastName = [self convertStringtoCharArray:lastName.text];
localUser.username = [self convertStringtoCharArray:username.text];
localUser.email = [self convertStringtoCharArray:emailAddress.text];
localUser.ipAddress = [self convertStringtoCharArray:localIPAddress.text];
localUser.latitude = currentLocation.coordinate.latitude;
localUser.longitude = currentLocation.coordinate.longitude;
//pass structure to be sent over socket
int result = [myNetworkConnection registerWithServer:&localUser];
NSString *message = nil;
//process result of sending attempt
if (result == 0) {
//registration succesful
message = [NSString stringWithString:#"Registration successful"];
} else if (result == 1) {
//server unavailable
message = [NSString stringWithString:#"Server unavailable. Please check your wi-fi settings and try again."];
} else if (result == 2) {
//unable to establish connection
message = [NSString stringWithString:#"Unable to communicate with server. Please check your wi-fi settings and try again."];
} else if (result == 3) {
//username already in use
message = [NSString stringWithString:#"Username in use. Try another username."];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Registration"
message:message
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
When I execute the code the iPhone greys out like it is about to display an alert but crashes. I get a EXC_BAD_ACCESS error in the console. Am I not releasing either the alert or the message correctly? Here is the console output:
Program received signal: “EXC_BAD_ACCESS”.
(gdb) backtrace
#0 0x30011944 in objc_msgSend ()
#1 0x3054803e in NSPopAutoreleasePool ()
#2 0x3054c808 in -[NSAutoreleasePool release] ()
#3 0x30936ac4 in _UIApplicationHandleEvent ()
#4 0x3204696c in PurpleEventCallback ()
#5 0x30254a76 in CFRunLoopRunSpecific ()
#6 0x3025416a in CFRunLoopRunInMode ()
#7 0x320452a4 in GSEventRunModal ()
#8 0x308f037c in -[UIApplication _run] ()
#9 0x308eea94 in UIApplicationMain ()
#10 0x000020bc in main (argc=1, argv=0x2ffff508) at /Users/reu2009/Documents/iPhone Development/Development/BuddyTracker/main.m:14
(gdb) frame 10
#10 0x000020bc in main (argc=1, argv=0x2ffff508) at /Users/reu2009/Documents/iPhone Development/Development/BuddyTracker/main.m:14 14 int retVal = UIApplicationMain(argc, argv, nil, nil);
Edit: removed [message release]; and assigned strings using [NSString stringWithString]; based on answers.
i had an issue like this ...i was calling uiAlertView from a background thread ....call it from the main thread
Objects returned from convenience constructors are already set to autorelease. While you declared a pointer to "message", the "message" object itself doesn't belong to you, since you used the #"string" convenience constructor to create the NSString object. Thus, you don't need to release it.
When you release it manually, it then gets released too many times (once manually, and once when the autorelease process rolls around) and throws the error.
Here's some additional information from Apple:
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html
Good rule of thumb: unless you use one of the alloc or init or copy methods to create an object (or if you retain the object yourself) you don't need to release it, but can rely on the method that actually created it to do that work for you.
Try it with NSZombieEnabled = YES.
Go into the Info of you Executable.
Click on the Arguments tab.
Click + on "Variables to be set in the environment."
Type NSZombieEnable and YES.
When the memory is released that has already been released, NSZombie will display the address, then you can use Instruments to find the actual object. Corbin's Treehouse has a good overview of how to do this:
Instruments on Leopard: How to debug those random crashes in your Cocoa app
Sean is right - you don't need to call [message release] here, because you're never actually retaining the message object.
Instead of just saying message = #"string", you need to say message = [NSString stringWithString:#"string"]; To be completely honest I'm not sure why (maybe someone can comment and I can improve this post!) but that should do the trick.
I had the same problem here with a UIAlertView, in my case, I had another class implementing the alert, and, from another one I was calling a static method. Like the following:
ClassA
...
doSomething {
... some stuff ...
[MyAlertView showAlert];
... some other stuff...
}
What I suspect is that, as the alertview is shown asynchronously when I clicked the button the object was already released.
To verify that, I changed the code to instantiate the alert and not release it. And everythong worked.
My final solution was to declare a variable in the parent view, and deallocate it with the other variables when the view is deallocated.
This could case due to updating UIKit from background thread
I solved it like this
UIAlertView *alertMSG = [[UIAlertView alloc] initWithTitle:nil
message:#"Your mnessage here"
delegate:self
cancelButtonTitle:#"Title here"
otherButtonTitles: nil];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[alertMSG show];
}];
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:#""]];
}
You are only guaranteed to retain access to a managed object from the context as long as a change is pending to that object (insert, update, delete). As soon as you make a call to save:, you can lose your reference to the managed object.
While you stopped getting the error when you set setRetainsRegisteredObjects:YES, you may have introduced a memory management issue, as you are setting the managed object's lifetime to be dependent on the lifetime of the managed object context. If you're passing your context around throughout your app, this could get rather large if you have a large object hierarchy.
You can read more in the Apple docs here: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/MO_Lifecycle.html
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.
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.
I have seen many funny behaviours with CoreData on iPhone that were solved by resetting content and settings in the iPhone simulator.
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.
You must set the FRC cache to nil
I have solved a similar problem by making sure that the object is fetched, so for the example above:
if ( message == nil ) {
message = [NSEntityDescription insertNewObjectForEntityForName:#"MailMessage" inManagedObjectContext:self.managedObjectContext];
} else {
NSError *error = nil;
message = (MailMessage *)[managedObjectContext existingObjectWithID:[message objectID] error:&error];
}