jamesmontemagno / MediaPlugin

Take & Pick Photos and Video Plugin for Xamarin and Windows
MIT License
711 stars 360 forks source link

[iOS] iOS 14, ObjectDisposedException MediaPickerController #910

Closed Maxima078 closed 2 years ago

Maxima078 commented 3 years ago

An ObjectDisposedException is thrown when taking photo with TakePhotoAsync. However it doesn't crash and the photo can still be used.

Here is exception details: System.ObjectDisposedException Message=Cannot access a disposed object. Object name: 'MediaPickerController'.

As you proposed I tried to migrate to Xamarin.essentials.MediaPicker unfortunatly it is currently missing options (at least AllowCropping, DefaultCamera and RotateImage) and the multiple pictures pick is not proposed.

Bug Information

Version Number of Plugin: 5.0.1 Device Tested On: iPhone XR Version of VS: 16.7.7 Version of Xamarin: 16.7.000.463 Versions of other things you are using: Version of Xamarin;iOS: 14.2.0.12

Steps to reproduce the Behavior

Call TakePhotoAsync

Expected Behavior

No exception thrown

Actual Behavior

An ObjectDisposedException is thrown

Screenshots

image

alsed42 commented 3 years ago

Had a similar issue (probably the same) with the following stacktrace:

    Type: System.ObjectDisposedException
    Message: "Cannot access a disposed object.
Object name: 'Foundation.InternalNSNotificationHandler'."
    Stacktrace:
      at ObjCRuntime.ThrowHelper.ThrowObjectDisposedException (System.Object o) [0x00000] in /Users/xyz/_work/1/s/xamarin-macios/src/ObjCRuntime/ThrowHelper.cs:34 
      at ObjCRuntime.NativeObjectExtensions.GetNonNullHandle (ObjCRuntime.INativeObject self, System.String argumentName) [0x0001b] in /Users/xyz/_work/1/s/xamarin-macios/src/ObjCRuntime/INativeObject.cs:29 
      at Foundation.NSNotificationCenter.RemoveObserver (Foundation.NSObject observer) [0x00000] in /Users/xyz/_work/1/s/xamarin-macios/src/build/ios/native/Foundation/NSNotificationCenter.g.cs:179 
      at Plugin.Media.MediaPickerDelegate.RemoveOrientationChangeObserverAndNotifications () [0x00012] in d:\a\1\s\src\Media.Plugin\iOS\MediaPickerDelegate.cs:197 
      at Plugin.Media.MediaPickerDelegate.Canceled (UIKit.UIImagePickerController picker) [0x00000] in d:\a\1\s\src\Media.Plugin\iOS\MediaPickerDelegate.cs:100 
      at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
      at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/xyz/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:86 
      at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Users/xyz/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:65 
      at MyApplication.iOS.Application.Main (System.String[] args) [0x00024] in C:\SVN\MyApplication\MyApplication.iOS\Main.cs:19 

Versions: Xam.Plugin.Media: 5.0.1 iOS: 14.4 Xamarin.Forms: 5.0.0.2012 Visual Studio: 16.9.1 IDB: 16.9.000.271 Device: iPhone 12 Pro (Simulator)

zackrykalash commented 3 years ago

Had a metadata exception issue, when i set SaveMetaData = true, i got System.ObjectDisposedException Message=Cannot access a disposed object. Object name: 'MediaPickerController'.

So same issue, i don't know how to work around it

15mgm15 commented 3 years ago

@zackrykalash Did you find a workaround for this?

LennartIZIT commented 3 years ago

Anyone who has a workaround for this? I'm still having this issue.

pdonovan commented 3 years ago

I'm now getting similar errors in our crash tracking:

Xamarin Exception Stack: System.ObjectDisposedException: Cannot access a disposed object. Object name: 'Foundation.InternalNSNotificationHandler'. at ObjCRuntime.ThrowHelper.ThrowObjectDisposedException (System.Object o) <0x103d76f5c + 0x00054> in <149d342757e54bfaa120bb42afe18369#cfbb9aa085457b8b669edc2e5b775274>:0 at ObjCRuntime.NativeObjectExtensions.GetNonNullHandle (ObjCRuntime.INativeObject self, System.String argumentName) <0x103d76350 + 0x0009b> in <149d342757e54bfaa120bb42afe18369#cfbb9aa085457b8b669edc2e5b775274>:0 at Foundation.NSNotificationCenter.RemoveObserver (Foundation.NSObject observer) <0x103dfe1b4 + 0x0003b> in <149d342757e54bfaa120bb42afe18369#cfbb9aa085457b8b669edc2e5b775274>:0 at Plugin.Media.MediaPickerDelegate.RemoveOrientationChangeObserverAndNotifications () <0x105d9d49c + 0x00037> in <bca0256229394a65ac3cb60c690728de#cfbb9aa085457b8b669edc2e5b775274>:0 at Plugin.Media.MediaPickerDelegate.FinishedPickingMedia (UIKit.UIImagePickerController picker, Foundation.NSDictionary info) <0x105d8fb20 + 0x000eb> in <bca0256229394a65ac3cb60c690728de#cfbb9aa085457b8b669edc2e5b775274>:0 at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) <0x102f811c4 + 0x0006b> in <218435b0f6654ac1ab0cdd176f26b9bd#cfbb9aa085457b8b669edc2e5b775274>:0 at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () <0x103e04c88 + 0x00037> in <149d342757e54bfaa120bb42afe18369#cfbb9aa085457b8b669edc2e5b775274>:0 at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr) at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) <0x103da420c + 0x00067> in <149d342757e54bfaa120bb42afe18369#cfbb9aa085457b8b669edc2e5b775274>:0

This was on an iPhone X, iOS 14.7.1 Xam.Plugin.Media: 5.0.1 Xamarn.Forms: 5.0.0.2125

jamesmontemagno commented 2 years ago

I believe I have this fixed in next beta

saamerm commented 2 years ago

@jamesmontemagno tried the latest pre-release version and it has the same issue. It works with te simulator but not with a device. The only difference between my code and the sample is that I am using the compression quality property Detailed stack trace:

at Foundation.NSObject.get_SuperHandle () [0x00023] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSObject2.cs:558 
  at UIKit.UIImagePickerController.get_Delegate () [0x0002a] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/ios/native/UIKit/UIImagePickerController.g.cs:351 
  at Plugin.Media.MediaPickerController.get_Delegate () [0x00000] in D:\a\1\s\src\Media.Plugin\iOS\MediaPickerController.cs:27 
  at Plugin.Media.MediaPickerController.<Dispose>b__6_0 () [0x00000] in D:\a\1\s\src\Media.Plugin\iOS\MediaPickerController.cs:56
image

Application output showed me this

CoreData: XPC:  Unable to create NSXPCConnection
CoreData: fault: Something has gone badly awry initializing the XPC connection pool: *** -[__NSArrayM insertObject:atIndex:]: object cannot be nil
2022-03-07 01:11:28.653 G20YEA.iOS[1788:201123] CoreData: XPC: sendMessage: failed #0
2022-03-07 01:11:28.653 G20YEA.iOS[1788:201123] CoreData: XPC: sendMessage: failed #1
2022-03-07 01:11:31.658 G20YEA.iOS[1788:201123] CoreData: XPC: sendMessage: failed #2
2022-03-07 01:11:31.659 G20YEA.iOS[1788:201123] CoreData: XPC: Unable to sendMessage: to server
2022-03-07 01:11:34.663 G20YEA.iOS[1788:201123] CoreData: XPC: sendMessage: failed #3
2022-03-07 01:11:37.669 G20YEA.iOS[1788:201123] CoreData: XPC: sendMessage: failed #4
2022-03-07 01:11:40.674 G20YEA.iOS[1788:201123] CoreData: XPC: sendMessage: failed #5
2022-03-07 01:11:43.677 G20YEA.iOS[1788:201123] CoreData: XPC: sendMessage: failed #6
2022-03-07 01:11:46.682 G20YEA.iOS[1788:201123] CoreData: XPC: sendMessage: failed #7
2022-03-07 01:11:49.686 G20YEA.iOS[1788:201123] CoreData: XPC: Unable to connect to server with options {
    NSXPCStoreServerEndpointFactory = "<PLXPCPhotoLibraryStoreEndpointFactory: 0x281c14d40>";
    skipModelCheck = 1;
}
2022-03-07 01:11:49.687 G20YEA.iOS[1788:201123] CoreData: XPC: Unable to load metadata: Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={Problem=Unable to send to server; failed after 8 attempts.}
CoreData: error: addPersistentStoreWithType:configuration:URL:options:error: returned error NSCocoaErrorDomain (134060)
CoreData: annotation: userInfo:
CoreData: annotation:   Problem : Unable to send to server; failed after 8 attempts.
CoreData: annotation: storeType: NSXPCStore
CoreData: annotation: configuration: (null)
CoreData: annotation: URL: file:///var/mobile/Media/PhotoData/Photos.sqlite
CoreData: annotation: options:
CoreData: annotation:   NSXPCStoreServerEndpointFactory : <PLXPCPhotoLibraryStoreEndpointFactory: 0x281c14d40>
CoreData: annotation:   skipModelCheck : 1
saamerm commented 2 years ago

I cloned the project and used the library directly to try to dig into the source and this is where the error comes and why the app doesn't crash:

image

This seems to be the step before:

image
pspeybro commented 2 years ago

Is there any workaround or solution for this? I see that there 5.1.1-beta version, but the changelog does not include the changes yet. @saamerm , did you check if it occurs without compression quality property?

saamerm commented 2 years ago

@pspeybro no workaround that I have found yet, if you do please let me know too! :) I havent tried removing compression quality, since compression to reduce file size is why I can't use the alternative xamarin essentials one, as mentioned by @Maxima078

flipper09112 commented 2 years ago

Hi! Some update?

callumideagen commented 2 years ago

Hello, Any update on this issue? Cannot use xamarin essentials as we need to support image compression :( .

volcano619 commented 2 years ago

Had a similar issue (probably the same) with the following stacktrace:

    Type: System.ObjectDisposedException
    Message: "Cannot access a disposed object.
Object name: 'Foundation.InternalNSNotificationHandler'."
    Stacktrace:
      at ObjCRuntime.ThrowHelper.ThrowObjectDisposedException (System.Object o) [0x00000] in /Users/xyz/_work/1/s/xamarin-macios/src/ObjCRuntime/ThrowHelper.cs:34 
      at ObjCRuntime.NativeObjectExtensions.GetNonNullHandle (ObjCRuntime.INativeObject self, System.String argumentName) [0x0001b] in /Users/xyz/_work/1/s/xamarin-macios/src/ObjCRuntime/INativeObject.cs:29 
      at Foundation.NSNotificationCenter.RemoveObserver (Foundation.NSObject observer) [0x00000] in /Users/xyz/_work/1/s/xamarin-macios/src/build/ios/native/Foundation/NSNotificationCenter.g.cs:179 
      at Plugin.Media.MediaPickerDelegate.RemoveOrientationChangeObserverAndNotifications () [0x00012] in d:\a\1\s\src\Media.Plugin\iOS\MediaPickerDelegate.cs:197 
      at Plugin.Media.MediaPickerDelegate.Canceled (UIKit.UIImagePickerController picker) [0x00000] in d:\a\1\s\src\Media.Plugin\iOS\MediaPickerDelegate.cs:100 
      at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
      at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/xyz/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:86 
      at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Users/xyz/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:65 
      at MyApplication.iOS.Application.Main (System.String[] args) [0x00024] in C:\SVN\MyApplication\MyApplication.iOS\Main.cs:19 

Versions: Xam.Plugin.Media: 5.0.1 iOS: 14.4 Xamarin.Forms: 5.0.0.2012 Visual Studio: 16.9.1 IDB: 16.9.000.271 Device: iPhone 12 Pro (Simulator)

I'm getting a similar issue any luck with a workaround?

saamerm commented 2 years ago

If anyone wants to give a stab at fixing this issue, let me know and I will give you all some pointers! @volcano619 @callumideagen @flipper09112 @pspeybro !

Essentially, (save your changes first), then use the debuggable version of this library:

  1. clone this repository and
  2. add the CSProj from here https://github.com/jamesmontemagno/MediaPlugin/tree/master/src/Media.Plugin into your own solution.
  3. Edit references for each of your projects and include this project as a reference
  4. Remove the MediaPlugin nuget package you have installed
  5. Try to build and you should have no issues building

Then during the runtime

  1. Place an exception catch-point and when you select your image, you should see the exception is caught on Line 56 of the MediaPickerController in the iOS project
  2. Try to fix the bug by playing around with the code.
  3. Once you get there, let me know what you have tried!
bijington commented 2 years ago

@saamerm I am currently digging into this issue. Sadly an end user has seen it but I am yet to reproduce it in a development environment. Is it specific to a Pro device?

bijington commented 2 years ago

@saamerm I am currently digging into this issue. Sadly an end user has seen it but I am yet to reproduce it in a development environment. Is it specific to a Pro device?

@saamerm I have opened a draft PR with some experimental changes in it. I haven't been able to reliably reproduce the issue but as I mention in my PR the changes don't appear to make things any worse.

Is there anyone that can reliably reproduce this that could test these changes out?

volcano619 commented 2 years ago

@bijington I can check, I'll report back if I'm able to reproduce it

bijington commented 2 years ago

@volcano619 thanks. I can confirm that so far my end user seeing this issue is unable to do so with the changes introduced. I must admit even they couldn't reproduce it regularly though