From f532b9f3f1d6f5368bbd12470f019f55f1007a59 Mon Sep 17 00:00:00 2001 From: Shaun Lawrence Date: Thu, 22 Sep 2022 11:00:03 +0100 Subject: [PATCH] Added locking around the disposal of the observer --- src/Media.Plugin/iOS/MediaPickerDelegate.cs | 25 +++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Media.Plugin/iOS/MediaPickerDelegate.cs b/src/Media.Plugin/iOS/MediaPickerDelegate.cs index 34158a4..23b35b0 100644 --- a/src/Media.Plugin/iOS/MediaPickerDelegate.cs +++ b/src/Media.Plugin/iOS/MediaPickerDelegate.cs @@ -154,6 +154,7 @@ public void DisplayPopover(bool hideFirst = false) UIDeviceOrientation? orientation; NSObject observer; + readonly object observerDisposeLock = new object(); readonly UIViewController viewController; readonly UIImagePickerControllerSourceType source; TaskCompletionSource> tcs = new TaskCompletionSource>(); @@ -203,18 +204,24 @@ void RemoveOrientationChangeObserverAndNotifications() if (observer != null) { - try - { - NSNotificationCenter.DefaultCenter.RemoveObserver(observer); - } - catch (Exception ex) + lock (observerDisposeLock) { - Debug.WriteLine(ex); - } + if (observer != null) + { + try + { + NSNotificationCenter.DefaultCenter.RemoveObserver(observer); + } + catch (Exception ex) + { + Debug.WriteLine(ex); + } + observer.Dispose(); + observer = null; + } + } } - observer?.Dispose(); - observer = null; } void DidRotate(NSNotification notice)