Repository navigation
Description
Current behavior 🐛
On Skia macOS, calling FileOpenPicker.PickMultipleFilesAsync() and pressing Cancel in the native NSOpenPanel raises System.AccessViolationException on the managed-side pointer walk:
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Runtime.InteropServices.Marshal.ReadInt64(IntPtr ptr, Int32 ofs)
at System.Runtime.InteropServices.Marshal.ReadIntPtr(IntPtr ptr)
at Uno.UI.Runtime.Skia.MacOS.MacOSFileOpenPickerExtension.PickMultipleFilesAsync(CancellationToken token)
at Windows.Storage.Pickers.FileOpenPicker.PickMultipleFilesTaskAsync(CancellationToken token) in C:\a\1\s\src\Uno.UWP\Storage\Pickers\FileOpenPicker.skia.cs:line 37
at Windows.Foundation.AsyncOperation`1.BuildTaskAsync(FuncAsync`2 taskBuilder) in C:\a\1\s\src\Uno.Foundation\Internal\AsyncOperation.TResult.cs:line 70
at System.WindowsRuntimeSystemExtensions.AsTaskCore[TResult](IAsyncOperation`1 source, CancellationToken ct) in C:\a\1\s\src\Uno.UWP\System.Runtime\WindowsRuntimeSystemExtensions.cs:line 66
at FilePickerCancelRepro.MainPage.OnPickClicked(Object sender, RoutedEventArgs e)
The AV fires at MacOSFileOpenPickerExtension.cs:84: the if (array == IntPtr.Zero) guard on L78 does not fire, and the subsequent Marshal.ReadIntPtr(array) dereferences invalid memory. PickSingleFileAsync() does not exhibit this on cancel — the string?-returning P/Invoke is null-marshalled and its null check works.
Expected behavior 🎯
Pressing Cancel should resolve the task with an empty list (or null), matching the WinUI contract and matching PickSingleFileAsync() on the same platform.
How to reproduce it (as minimally and precisely as possible) 🔬
Minimal repro: https://github.com/jpnurmi/uno-macos-filepicker-cancel-repro
git clone https://github.com/jpnurmi/uno-macos-filepicker-cancel-repro
cd uno-macos-filepicker-cancel-repro
dotnet run --project FilePickerCancelRepro -f net10.0-desktop
Click Pick files..., press Cancel in the NSOpenPanel → AccessViolationException.
Or scaffold a blank Uno desktop app:
dotnet new unoapp -n FilePickerCancelRepro -tfm net10.0 -platforms desktop -preset blank -markup xaml
MainPage.xaml:
<Page x:Class="FilePickerCancelRepro.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Spacing="12">
<Button Content="Pick files..." Click="OnPickClicked" />
<TextBlock x:Name="ResultText" TextWrapping="Wrap" MaxWidth="600" />
</StackPanel>
</Page>MainPage.xaml.cs:
using Windows.Storage.Pickers;
namespace FilePickerCancelRepro;
public sealed partial class MainPage : Page
{
public MainPage() => this.InitializeComponent();
private async void OnPickClicked(object sender, RoutedEventArgs e)
{
try
{
var picker = new FileOpenPicker { ViewMode = PickerViewMode.List };
picker.FileTypeFilter.Add("*");
var files = await picker.PickMultipleFilesAsync();
ResultText.Text = files?.Count > 0 ? $"Picked {files.Count}" : "Cancelled.";
}
catch (Exception ex)
{
ResultText.Text = $"{ex.GetType().FullName}: {ex.Message}\n{ex.StackTrace}";
}
}
}Run, click Pick files..., press Cancel in the NSOpenPanel → AccessViolationException.
Workaround 🛠️
Catch AccessViolationException around the call and treat it as cancellation:
try
{
files = await picker.PickMultipleFilesAsync();
}
catch (AccessViolationException)
{
return Array.Empty<StorageFile>();
}Renderer 🎨
- Skia
- Native
Affected platforms 📱💻🖥️
- Desktop (macOS)
Uno.Sdk version (and other relevant versions) 📦
- Uno.Sdk 6.5.31
- Target framework: net10.0-desktop
- macOS 26.3.1 (Apple Silicon)
IDE version 🧑💻
Not IDE-specific — reproduced from the command line via dotnet run.
Anything else we need to know? 💬
Likely suspects (both worth inspecting together):
- Managed —
MacOSFileOpenPickerExtension.cs:78-94: only guards againstarray == IntPtr.Zero. Any non-null, non-NULL-terminated pointer walks into invalid memory. - Native —
UNOPickers.m:129-166: the cancel branch advertisesreturn nil;, but the managed-side AV strongly suggests the returned pointer is not actually zero on this path under current macOS SDKs (shippedlibUnoNativeMac.dylibis not built withDEBUG, so itsNSLogprobes are silent — can't confirm from logs alone).
Unrelated but adjacent: PickSingleFileAsync on the same platform is separately affected by #22307 — different stack, different cause.
Activity
Add minimal repro for unoplatform/uno#23071