-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dispose empty sets and try/catch exceptions #10955
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.Versioning; | ||
using Foundation; | ||
using Microsoft.Extensions.Logging; | ||
|
@@ -57,14 +58,23 @@ public static void CreatePlatformWindow(this IUIWindowSceneDelegate sceneDelegat | |
dicts.Add(session.UserInfo); | ||
if (session.StateRestorationActivity?.UserInfo is not null) | ||
dicts.Add(session.StateRestorationActivity.UserInfo); | ||
if (connectionOptions.UserActivities is not null) | ||
try | ||
{ | ||
foreach (var u in connectionOptions.UserActivities) | ||
using var activities = connectionOptions.UserActivities; | ||
if (activities is not null) | ||
{ | ||
if (u is NSUserActivity userActivity && userActivity.UserInfo is not null) | ||
dicts.Add(userActivity.UserInfo); | ||
foreach (var u in activities) | ||
{ | ||
if (u is NSUserActivity userActivity && userActivity.UserInfo is not null) | ||
dicts.Add(userActivity.UserInfo); | ||
} | ||
} | ||
} | ||
catch (InvalidCastException) | ||
{ | ||
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704 | ||
// This only throws if the collection is empty. | ||
} | ||
Comment on lines
+73
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Second-point-five protection, if we do somehow throw, then we know there is no user activity. |
||
|
||
var window = CreatePlatformWindow(application, scene as UIWindowScene, dicts.ToArray()); | ||
if (window is not null) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,9 +101,18 @@ public void Init(Func<UIViewController?>? getCurrentUIViewController) => | |
// if we have scene support, use that | ||
if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13)) | ||
{ | ||
var scenes = UIApplication.SharedApplication.ConnectedScenes; | ||
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault(); | ||
return windowScene?.Windows.FirstOrDefault(); | ||
try | ||
{ | ||
using var scenes = UIApplication.SharedApplication.ConnectedScenes; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First level of attack, make sure to dispose the "other" set so that there is no managed object around to get a miss-type. |
||
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault(); | ||
return windowScene?.Windows.FirstOrDefault(); | ||
} | ||
catch (InvalidCastException) | ||
{ | ||
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704 | ||
// This only throws if the collection is empty. | ||
return null; | ||
} | ||
Comment on lines
+110
to
+115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Second level, if we do get an exception, we know it is because the set was empty, so do empty things. |
||
} | ||
|
||
// use the windows property (up to 13.0) | ||
|
@@ -115,9 +124,18 @@ public void Init(Func<UIViewController?>? getCurrentUIViewController) => | |
// if we have scene support, use that | ||
if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13)) | ||
{ | ||
var scenes = UIApplication.SharedApplication.ConnectedScenes; | ||
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault(); | ||
return windowScene?.Windows; | ||
try | ||
{ | ||
using var scenes = UIApplication.SharedApplication.ConnectedScenes; | ||
var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault(); | ||
return windowScene?.Windows; | ||
} | ||
catch (InvalidCastException) | ||
{ | ||
// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704 | ||
// This only throws if the collection is empty. | ||
return null; | ||
} | ||
} | ||
|
||
// use the windows property (up to 15.0) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First-point-five level, make sure we also dispose the error set because there may be another set later on.