Bug 7443 - MFMailComposeViewController crashes
Summary: MFMailComposeViewController crashes
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 6.0.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
URL:
: 7446 ()
Depends on:
Blocks:
 
Reported: 2012-09-25 09:27 UTC by Nic Wise
Modified: 2012-09-26 09:39 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
hotfix (4.05 MB, application/octet-stream)
2012-09-25 22:05 UTC, Sebastien Pouliot
Details
Project InAppPurchase sample with Same MFMailCOmposer bug (795.44 KB, application/octet-stream)
2012-09-26 08:37 UTC, Emanuele Sabetta
Details
InAppPurchase test project - full folder zip (3.68 MB, application/octet-stream)
2012-09-26 08:46 UTC, Emanuele Sabetta
Details
the single modified .cs file of the InAppPurchase sample (10.24 KB, application/octet-stream)
2012-09-26 09:06 UTC, Emanuele Sabetta
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Nic Wise 2012-09-25 09:27:01 UTC
I've having issues narrowing this down, as a new project works, however, this crashes:

(mailCompose is a class-level static variable, but I have tried it local too)

mailCompose = new MFMailComposeViewController();
				
with this error:

2012-09-25 14:16:29.637 MicroAgent[1701:5203] *** Assertion failure in NSDictionary *_UIRecordArgumentOfInvocationAtIndex(NSInvocation *, NSUInteger, BOOL)(), /SourceCache/UIKit_Sim/UIKit-2372/UIAppearance.m:1118

Native stacktrace:

	0   MicroAgent                          0x000c142c mono_handle_native_sigsegv + 284
	1   MicroAgent                          0x00036212 mono_sigsegv_signal_handler + 178
	2   libsystem_c.dylib                   0x96c7886b _sigtramp + 43
	3   ???                                 0xffffffff 0x0 + 4294967295
	4   MicroAgent                          0x0019beae mono_runtime_invoke + 126
	5   MicroAgent                          0x00245b2f monotouch_exception_handler + 63
	6   CoreFoundation                      0x042db318 __handleUncaughtException + 728
	7   libobjc.A.dylib                     0x044880b9 _ZL15_objc_terminatev + 86
	8   libstdc++.6.dylib                   0x04a2a652 _ZSt9terminatev + 21
	9   libobjc.A.dylib                     0x04488039 _destroyAltHandlerList + 0
	10  libdispatch.dylib                   0x04b29027 _dispatch_client_callout + 33
	11  libdispatch.dylib                   0x04b18d5f _dispatch_barrier_sync_f_invoke + 58
	12  libdispatch.dylib                   0x04b18aa3 dispatch_barrier_sync_f + 108
	13  libdispatch.dylib                   0x04b18e5e dispatch_sync + 45
	14  UIKit                               0x01d5dfad __76-[_UIRemoteViewControllerConnectionRequest _connectToViewControllerOperator]_block_invoke_0 + 105
	15  UIKit                               0x01d69e46 __block_global_0 + 54
	16  libdispatch.dylib                   0x04b29014 _dispatch_client_callout + 14
	17  libdispatch.dylib                   0x04b1b09f dispatch_once_f + 57
	18  libdispatch.dylib                   0x04b1b061 dispatch_once + 31
	19  UIKit                               0x01d69e0a __73-[_UIViewServiceSessionEndpoint connectToDeputyOfType:connectionHandler:]_block_invoke_0 + 105
	20  UIKit                               0x01d6a002 __73-[_UIViewServiceSessionEndpoint connectToDeputyOfType:connectionHandler:]_block_invoke_040 + 25
	21  UIKit                               0x01dc74e9 __block_global_0 + 32
	22  libdispatch.dylib                   0x04b1753f _dispatch_call_block_and_release + 15
	23  libdispatch.dylib                   0x04b29014 _dispatch_client_callout + 14
	24  libdispatch.dylib                   0x04b19418 _dispatch_queue_drain + 239
	25  libdispatch.dylib                   0x04b192a6 _dispatch_queue_invoke + 59
	26  libdispatch.dylib                   0x04b1a280 _dispatch_root_queue_drain + 231
	27  libdispatch.dylib                   0x04b1a450 _dispatch_worker_thread2 + 39
	28  libsystem_c.dylib                   0x96c8ee12 _pthread_wqthread + 441
	29  libsystem_c.dylib                   0x96c76cca start_wqthread + 30

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

This is also here on SO: 

http://stackoverflow.com/questions/12567708/monotouch-ios-6-crash-when-using-mfmailcomposeviewcontroller

try/catch around the new doesn't help. Crashes out before that.

In a brand new project, this works fine tho. This project has been around for a while....
Comment 1 Nic Wise 2012-09-25 09:36:06 UTC
I just tried this on my wife's phone and it works fine on iOS 5.1.1, but not on my new iOS6 iPhone 5. Or in the iOS6 SIM
Comment 2 Nic Wise 2012-09-25 10:01:59 UTC
and.... just tried it on another app, and the same code works fine (thats also an old app). It's just this one. You know how to get me if you want source - it's on bitbucket.
Comment 3 Sebastien Pouliot 2012-09-25 11:33:24 UTC
*** Bug 7446 has been marked as a duplicate of this bug. ***
Comment 4 Sebastien Pouliot 2012-09-25 11:38:32 UTC
The Objective-C assertion, from UIAppearance.m:1118, makes me think the issue occurs on MFMailComposeViewController only if something else was done earlier, e.g. setting some UIAppearance values.

Nic, might that might explain why this works for some of your apps and not for others ?

I'll try to duplicate this locally (our test bots might now return true for CanSendMail so that could hide the issue).
Comment 5 Nic Wise 2012-09-25 11:43:07 UTC
I can turn off UIAppearance easily enough. Just a second.
Comment 6 Nic Wise 2012-09-25 11:48:05 UTC
Correct, sir. This breaks it (AppearanceManager.cs)


				UITextAttributes cancelAttributes = new UITextAttributes();
				cancelAttributes.TextShadowOffset = new UIOffset(0,-1);
				cancelAttributes.TextShadowColor = UIColor.White.ColorWithAlpha(0.8f);
				cancelAttributes.TextColor = Resources.CancelButtonEnabled;
				
				UIBarButtonItem.AppearanceWhenContainedIn(typeof(UISearchBar)).SetTitleTextAttributes(cancelAttributes, UIControlState.Normal);

				cancelAttributes = new UITextAttributes();
				cancelAttributes.TextShadowOffset = new UIOffset(0, -1);
				cancelAttributes.TextShadowColor = UIColor.White.ColorWithAlpha(0.5f);
				cancelAttributes.TextColor = Resources.CancelButtonDisabled;
				
				UIBarButtonItem.AppearanceWhenContainedIn(typeof(UISearchBar)).SetTitleTextAttributes(cancelAttributes, UIControlState.Disabled);


comment that out,and it works fine. So... WHY?
Comment 8 Nic Wise 2012-09-25 12:22:38 UTC
I've found the one line:

cancelAttributes.TextShadowOffset = new UIOffset(0, -1);

I'm sure I've had a conversation about this with you ages ago. But I can't find what or where. I might just remove that line and be done with it, but thats not good for MonoTouch :)
Comment 9 Sebastien Pouliot 2012-09-25 12:49:52 UTC
I don't recall that conversation... (blame my neurons ;-) but shadowOffset did ring a (recent) bell:

> It is not supported to set values for the shadowOffset or shadowColor properties of a UILabel object if its attributedText property contains a valid attributed string. Instead, use the NSShadowAttributeName attribute of the attributed string to set the shadow.

ref: https://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html#//apple_ref/doc/uid/TP40012166

I wonder is iOS6 could be using attributedText, internally, for MFMailComposeViewController... in any case more investigation is needed but a huge thanks for spotting the right lines :-)

Emanuele, does your application uses similar appearance code ?
Comment 10 Emanuele Sabetta 2012-09-25 13:02:36 UTC
Unfortunately not. I commented out every Attributes code but my app still crashes. :(
Comment 11 Sebastien Pouliot 2012-09-25 14:20:17 UTC
> I commented out every Attributes

is that a typo ?

The first step should be to comment all uses Appearance.

If it works (without using Appearance) then try to re-enable some of them (like a binary search) until you see which Appearance-related API could result in the assertion (i.e. there could be other appearance, beside TextShadowOffset, that could trigger a similar assertion).
Comment 12 Emanuele Sabetta 2012-09-25 16:33:11 UTC
yes, it was a typo. I've already commented out all appearence related code as you did, but unfortunately thee crash doesn't go away. I tried to comment out all code lines that use attributed strings too, but no difference. It still crashes when i call the mail composer.
Now i'm trying to investigate if there is something in my code that uses the appearance class indirectly, like monotouch.dialog. But I doubt it.
Comment 13 Sebastien Pouliot 2012-09-25 21:45:16 UTC
It seems `UITextAttributeShadowOffset` returns null on iOS6. I don't recall if this was the case for earlier versions but I think this is likely the issue.
Comment 14 Sebastien Pouliot 2012-09-25 21:49:17 UTC
UITextAttributeShadowOffset
UITextAttributeTextShadowOffset

^ typo, it returns null on iOS 5.1 but it does not crash the application.
Comment 15 Sebastien Pouliot 2012-09-25 22:00:18 UTC
fixed.
master: 63cbe1f6d79184798989dc32a0be850ddbf061b5
ios6: c61fd71745bd7584cab2b6322512fb40ed0900a1
Comment 16 Sebastien Pouliot 2012-09-25 22:05:04 UTC
Created attachment 2616 [details]
hotfix

I assume you gentlemen would like an hotfix...

To use the attached assembly (on top of MonoTouch 6.0.1) do:

1) backup your /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll and /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll.mdb files

2) copy the attached file to /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll

3) remove the /Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll.mdb symbols (they won't match anymore)

4) clean, rebuild and test your application
Comment 17 Emanuele Sabetta 2012-09-26 08:08:43 UTC
Unfortunately It seems that there is more than one bug here.

In my case the crash of the Mail Composer was not caused by an Appearance or an attributed string. I tested all possible connection, but my code crashes.

So I started commenting all my code line by line, and I nailed it:

It's the the In App Purchase code.

I'm using the code from the In App Purchase sample on the xamarin website.

If I comment just this line using the SKProductRequest class:

productsRequest.Start();

The MFMailComposer open and works perfectly fine!

So I tested the MFMailComposer on the original Xamarin sample project:

http://docs.xamarin.com/ios/tutorials/In-App_Purchasing
http://docs.xamarin.com/@api/deki/files/3300/=InAppPurchaseSample.zip

I just added this code to the NonConsumableViewController class:

/// <summary>
		/// TEST MAIL COMPOSER BUG - START
		/// </summary>
		
		MFMailComposeViewController _mail;
		
		void HandleFinished (object sender, MFComposeResultEventArgs e)
		{
			
		}
		
		public void TestMailComposer ()
		{
			var button = UIButton.FromType(UIButtonType.RoundedRect);
			button.Frame = new RectangleF(20f, 0f, 280f, 40f);
			button.TouchUpInside += (object sender, EventArgs e) => {
				if (MFMailComposeViewController.CanSendMail) {
					_mail = new MFMailComposeViewController ();
					_mail.SetSubject ("The Subject");
					_mail.SetToRecipients (new [] {"the@recipient.com"});
					_mail.SetMessageBody ("Some text to e-mail stuff too" + "\n", false);
					_mail.Finished += HandleFinished;
					
					this.PresentViewController(_mail, true, null);
					
				} else {
					//handle not being able to send mail
					UIAlertView alert = new UIAlertView ("Mail Alert", "Error: Check your eMail configuration", null, "OK", null);
					alert.Show ();
				}
			};
			button.SetTitle("Button", UIControlState.Normal);
			View.AddSubview(button);
		}
		
		///
		/// TEST MAIL COMPOSER BUG - END
		///

And when i push the button it hangs!!!
Comment 18 Sebastien Pouliot 2012-09-26 08:28:42 UTC
It's weird that you got the same (appearance-related) assertion, but there could be other issues. 

* Did you try the attached hotfix ?

Adding the above code won't do anything since the code won't be called from anywhere. Please attach the modified sample to the bug report so we can be sure we're testing the same thing as you have (a small code difference can make huge result difference).

The fact that it hangs, not crash, suggest it could be something else.
Comment 19 Emanuele Sabetta 2012-09-26 08:37:25 UTC
Created attachment 2619 [details]
Project InAppPurchase sample with Same MFMailCOmposer bug
Comment 20 Emanuele Sabetta 2012-09-26 08:39:13 UTC
Of course I added a call to the TestMailComposer  method from the ViewDidLoad method.
It hangs in the simulator, but not on the device.
And you are right, the appearance related assertion error doesn't appear. But the behaviour is the same.
Comment 21 Sebastien Pouliot 2012-09-26 08:43:40 UTC
> * Did you try the attached hotfix ?

^ please :-)


> But the behaviour is the same.

A hang (on sim only) and a crash is not the same behaviour - or there's missing pieces of information in your report.


Also your attached project does not build as it seems to refer to files that were outside your archive.

error CS2001: Source file `/Users/poupou/Downloads/InAppPurchaseSample (2)/NonConsumables/AppDelegate.cs' could not be found
error CS2001: Source file `/Users/poupou/Downloads/InAppPurchaseSample (2)/NonConsumables/InAppPurchaseManager.cs' could not be found
error CS2001: Source file `/Users/poupou/Downloads/InAppPurchaseSample (2)/NonConsumables/SKProductExtension.cs' could not be found
error CS2001: Source file `/Users/poupou/Downloads/InAppPurchaseSample (2)/NonConsumables/CustomPaymentObserver.cs' could not be found
error CS2001: Source file `/Users/poupou/Downloads/InAppPurchaseSample (2)/NonConsumables/NonConsumableViewController.cs' could not be found
error CS2001: Source file `/Users/poupou/Downloads/InAppPurchaseSample (2)/NonConsumables/PhotoFilterManager.cs' could not be found
error CS2001: Source file `/Users/poupou/Downloads/InAppPurchaseSample (2)/NonConsumables/VerificationController.cs' could not be found
Comment 22 Emanuele Sabetta 2012-09-26 08:46:00 UTC
Created attachment 2620 [details]
InAppPurchase test project - full folder zip
Comment 23 Emanuele Sabetta 2012-09-26 08:47:11 UTC
The export function of MonoDevelop doesn't seem to work correctly. I zipped the entire folder manually and re uploaded it. Check it out.
Comment 24 Emanuele Sabetta 2012-09-26 09:06:57 UTC
Created attachment 2621 [details]
the single modified .cs file of the InAppPurchase sample
Comment 25 Emanuele Sabetta 2012-09-26 09:08:33 UTC
The files of the solutions are there but it seems that MonoDevelop is not able to export the second project folders path correctly.
I've uploadd the NonConsumableViewController.cs file above, the only one I modified. Check it out.
Comment 26 Emanuele Sabetta 2012-09-26 09:39:30 UTC
I tried the hotfix (sprry for the wait, but I had to revert my code to the original state). The appearance bug is gone, thank you. 

The problem with the SKProductRequest class seems less urgent. It doesn't hangs, it just delays the opening of the MF Mail composer for a while. But this seems an unrelated bug.