ListView.Items.Add throws ExecutionEngineException with custom ListViewItemSorter
See original GitHub issue-
.NET Core Version: 5.0.100
-
Have you experienced this same bug with .NET Framework?: No
Problem description:
ListView.Items.Add throws ExecutionEngineException upon reaching a certain number of items in the ListView when ListView.ListViewItemSorter is used to sort the items.
The exact number might vary on the specific use case and machine setup. The repro linked below crashes on my machine after ~130 added items, although I have observed numbers as low as 16 in a project of mine.
This didn’t happen in .NET Core 3.1.
Stack trace:
Process terminated. A callback was made on a garbage collected delegate of type 'System.Windows.Forms.Primitives!System.Windows.Forms.NativeMethods+ListViewCompareCallback::Invoke'.
Repeat 2 times:
--------------------------------
at Interop+User32.CallWindowProcW(IntPtr, IntPtr, WM, IntPtr, IntPtr)
--------------------------------
at System.Windows.Forms.NativeWindow.DefWndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.ListView.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.Control+ControlNativeWindow.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.NativeWindow.Callback(IntPtr, WM, IntPtr, IntPtr)
at Interop+User32.SendMessageW(IntPtr, WM, IntPtr, IntPtr)
at Interop+User32.SendMessageW(IntPtr, WM, IntPtr, IntPtr)
at System.Windows.Forms.ListView.Sort()
at System.Windows.Forms.ListView.InsertItems(Int32, System.Windows.Forms.ListViewItem[], Boolean)
at System.Windows.Forms.ListView+ListViewNativeItemCollection.Add(System.Windows.Forms.ListViewItem)
at System.Windows.Forms.ListView+ListViewItemCollection.Add(System.Windows.Forms.ListViewItem)
at ListViewRepro.MainForm.AddFruit()
at ListViewRepro.MainForm.MainForm_Shown(System.Object, System.EventArgs)
at System.Windows.Forms.Form.OnShown(System.EventArgs)
at System.Windows.Forms.Form.CallShownEvent()
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.ScrollableControl.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.Form.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.Control+ControlNativeWindow.WndProc(System.Windows.Forms.Message ByRef)
at System.Windows.Forms.NativeWindow.Callback(IntPtr, WM, IntPtr, IntPtr)
at Interop+User32.DispatchMessageW(MSG ByRef)
at Interop+User32.DispatchMessageW(MSG ByRef)
at System.Windows.Forms.Application+ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(UIntPtr, msoloop, Void*)
at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(msoloop, System.Windows.Forms.ApplicationContext)
at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(msoloop, System.Windows.Forms.ApplicationContext)
at System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
at ListViewRepro.Program.Main()
Expected behavior: ListView.Add does not throw ExecutionEngineException.
Minimal repro: https://github.com/dhrdlicka/ListViewRepro (The LVIS assignment is commented out in the MainForm constructor as the same project serves as repro on another issue as well)
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (12 by maintainers)
Hi @RussKie, yes, I’m able to reliably (100% of the time) reproduce the crash with the comparer code sample, running on .NET SDK 5.0.100 (x64) on Windows 10 Version 2004, when running the application compiled for Debug or Release in Visual Studio with and without a debugger. I haven’t tried yet with a nightly build.
dotnet --info
.NET SDK (gemäß "global.json"): Version: 5.0.100 Commit: 5044b93829
Laufzeitumgebung: OS Name: Windows OS Version: 10.0.19041 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\5.0.100\
Host (useful for support): Version: 5.0.0 Commit: cf258a14b7
.NET SDKs installed: 5.0.100-rc.2.20479.15 [C:\Program Files\dotnet\sdk] 5.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed: Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
To install additional .NET runtimes or SDKs: https://aka.ms/dotnet-download
Here’s a comlpete code sample with which I can reproduce the issue:
Program.cs
:App.csproj
:The
ExecutionEngineException
above doesn’t show any details in my case, but when I run the application without a debugger, an error is also logged to the EventLog (.NET Runtime):If it still doesn’t reproduce, you could try to increase the timeout in
Thread.Sleep()
, e.g. to 100 or 1000.@JeremyKuhne @weltkante do you think we’re failing to keep the
callback
alive, i.e.GC.KeepAlive(callback)
on line:5532? https://github.com/dotnet/winforms/blob/00b77530a64b0e0e1bfcc57e523f46c58e7c6843/src/System.Windows.Forms/src/System/Windows/Forms/ListView.cs#L5527-L5532