'xamarin native crash - when working with photo/file bytes
I have a simple xamarin app which captures photos, stores them locally and lets the user upload them to a web service later.
Its working perfectly in Android, but throws a native error in iOS. I've tried 2 different iPhones and iOS 15.3 and 15.3.1 with the same result. Also tried Xamarin.Essentials.MediaPicker.CapturePhotoAsync and Plugin.Media.CrossMedia.Current.TakePhotoAsync
I'm not sure who to report this to, or whether I'm doing something silly?
Here's how to reproduce...
In VisualStudio2022, new blank Xamarin app. Add CAMERA permissions to AndroidManifest.xml and Info.plist.
MainPage.xaml...
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PhotoTest.MainPage">
<StackLayout>
<Image x:Name="imgPhoto" HeightRequest="250" HorizontalOptions="CenterAndExpand" />
<Button Text="take photo" Clicked="OnButtonClick" />
</StackLayout>
</ContentPage>
MainPage.cs...
using System;
using System.IO;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace PhotoTest
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void OnButtonClick(object sender, EventArgs e)
{
FileResult photo = await MediaPicker.CapturePhotoAsync();
byte[] photoBytes;
using (var stream = await photo.OpenReadAsync())
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
photoBytes = memoryStream.ToArray();
}
}
// ... insert code here to store/send photoBytes
imgPhoto.Source = ImageSource.FromStream(() => { return new MemoryStream(photoBytes); });
// Android: code above works as expected
// IOS: native exception thrown after code above is executed ... no image shown in UI
}
}
}
Error log...
2022-02-14 14:46:42.569 Xamarin.PreBuilt.iOS[4033:2168079] Xamarin.iOS: Received unhandled ObjectiveC exception: NSInvalidArgumentException *** NSAllocateMemoryPages(4328713200) failed
2022-02-14 14:46:42.570 Xamarin.PreBuilt.iOS[4033:2168079] error: * Assertion: should not be reached at /Users/builder/jenkins/workspace/archive-mono/2020-02/ios/release/mono/mini/mini-exceptions.c:452
=================================================================
Native Crash Reporting
=================================================================
Got a segv while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
=================================================================
Native stacktrace:
=================================================================
0x1022e81e0 - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : mono_dump_native_crash_info
0x1022dec5c - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : mono_handle_native_crash
0x1022ebbb8 - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : mono_sigsegv_signal_handler_debug
0x1dc458d48 - /usr/lib/system/libsystem_platform.dylib : <redacted>
0x1834fc050 - /System/Library/Frameworks/Foundation.framework/Foundation : <redacted>
0x10248dc54 - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : do_icall
0x10248c2e4 - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : do_icall_wrapper
0x1024831d8 - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : interp_exec_method_full
0x102481768 - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : interp_runtime_invoke
0x1022eee70 - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : mono_jit_runtime_invoke
0x10239d188 - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : mono_runtime_try_invoke
0x1023d7948 - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : worker_callback
0x1023d51bc - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : worker_thread
0x1023e0144 - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : start_wrapper_internal
0x1023dffc8 - /private/var/containers/Bundle/Application/5CAAC91D-2B0B-4E2C-ACFF-75576C8D40C9/PhotoTest.iOS.app/Xamarin.PreBuilt.iOS : start_wrapper
0x1dc4693a4 - /usr/lib/system/libsystem_pthread.dylib : _pthread_start
0x1dc4679fc - /usr/lib/system/libsystem_pthread.dylib : thread_start
=================================================================
Basic Fault Address Reporting
=================================================================
Memory around native instruction pointer (0x1dc457974):
0x1dc457964
28
24 40
a9
2a
2c 41 a9 21 80 00 91 42 00 05 cb ($@.*,A.!...B...
0x1dc457974 0c 34 00
a8 0e 3c 01 a8 42 00 01 f1 29 01 00 54 .4...<..
B...)..T
0x1dc457984 68 24
00 a8 6a 2c 01 a8 63
80 00 91 28 24 40 a9 h$..j,..c...($@.
0x1dc457994 2a 2c 41 a9 21 80 00 91 42 80 00 f1 28
ff ff 54 *,A.!...B...(..T
=================================================================
Managed Stacktrace:
=================================================================
at <unknown> <0xffffffff>
at ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_UIntPtr <0x00022>
at Foundation.NSMutableData:AppendBytes <0x00054>
at Foundation.NSData:FromStream <0x0010c>
at <LoadImageAsync>d__1:MoveNext <0x00160>
at MoveNextRunner:InvokeMoveNext <0x00012>
at <Module>:invoke_void_object <0x0009e>
at System.Threading.ExecutionContext:RunInternal <0x00134>
at System.Threading.ExecutionContext:Run <0x00016>
at MoveNextRunner:Run <0x00058>
at <Module>:invoke_void <0x00078>
at System.Threading.Tasks.AwaitTaskContinuation:System.Threading.IThreadPoolWorkItem.ExecuteWorkItem <0x00020>
at System.Threading.ThreadPoolWorkQueue:Dispatch <0x0014c>
at System.Func`1:invoke_TResult <0x00096>
at ObjCRuntime.Runtime:ThreadPoolDispatcher <0x0001e>
at System.Func`2:invoke_TResult_T <0x0009e>
at System.Threading._ThreadPoolWaitCallback:PerformWaitCallback <0x00038>
at <Module>:runtime_invoke_direct_bool <0x00036>
=================================================================
Info.plist ...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIDeviceFamily</key>
<array>
<integer>1</integer>
<integer>2</integer>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>MinimumOSVersion</key>
<string>8.0</string>
<key>CFBundleDisplayName</key>
<string>PhotoTest</string>
<key>CFBundleIdentifier</key>
<string>com.companyname.PhotoTest</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>CFBundleName</key>
<string>PhotoTest</string>
<key>XSAppIconAssets</key>
<string>Assets.xcassets/AppIcon.appiconset</string>
<key>NSCameraUsageDescription</key>
<string>This app needs access to the camera to take photos.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app needs access to microphone for taking videos.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app needs access to the photo gallery for picking photos and videos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to photos gallery for picking photos and videos.</string>
</dict>
</plist>
Run the app via visual studio and hot reload (without a mac)
Solution 1:[1]
Since the app worked ok when deployed via mac / TestFlight I have to assume this is an issue currently with Visual Studio and Hot Restart.
Reported as a problem with Hot Restart to the Visual Studio Developer Community.
For more information about Hot Restart limitations check here...
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/deploy-test/hot-restart#limitations
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ryano |
