question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
kpreissercommented, Nov 24, 2020

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:

using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;

namespace repro_listview
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var f = new Form();
            var listView = new ListView();

            f.Controls.Add(listView);

            listView.Items.Add("A");
            listView.Items.Add("Z");
            listView.Items.Add("X");
            listView.ListViewItemSorter = new TestComparer();

            Application.Run(f);
        }

        class TestComparer : Comparer<ListViewItem>
        {
            public override int Compare(ListViewItem x, ListViewItem y)
            {
                GC.Collect();
                Thread.Sleep(10);
                return 0;
            }
        }
    }
}

App.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <RootNamespace>winf_listview</RootNamespace>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

grafik

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):

Application: winf-listview.exe
CoreCLR Version: 5.0.20.51904
.NET Version: 5.0.0
Description: The application requested process termination through System.Environment.FailFast(string message).
Message: A callback was made on a garbage collected delegate of type 'System.Windows.Forms.Primitives!System.Windows.Forms.NativeMethods+ListViewCompareCallback::Invoke'.
Stack:
   at Interop+User32.CallWindowProcW(IntPtr, IntPtr, WM, IntPtr, IntPtr)
   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.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.OnMessage(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(IHandle, WM, IntPtr, IntPtr)
   at System.Windows.Forms.ListView.Sort()
   at System.Windows.Forms.ListView.OnHandleCreated(System.EventArgs)
   at System.Windows.Forms.Control.WmCreate(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.OnMessage(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.CreateWindowExW(WS_EX, Char*, System.String, WS, Int32, Int32, Int32, Int32, IntPtr, IntPtr, IntPtr, System.Object)
   at Interop+User32.CreateWindowExW(WS_EX, Char*, System.String, WS, Int32, Int32, Int32, Int32, IntPtr, IntPtr, IntPtr, System.Object)
   at Interop+User32.CreateWindowExW(WS_EX, System.String, System.String, WS, Int32, Int32, Int32, Int32, IntPtr, IntPtr, IntPtr, System.Object)
   at System.Windows.Forms.NativeWindow.CreateHandle(System.Windows.Forms.CreateParams)
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.ListView.CreateHandle()
   at System.Windows.Forms.Control.CreateControl(Boolean)
   at System.Windows.Forms.Control.CreateControl(Boolean)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(System.Windows.Forms.Message ByRef)
   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.ContainerControl.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.Form.WmShowWindow(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.Form.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.Control+ControlNativeWindow.OnMessage(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.ShowWindow(IntPtr, SW)
   at Interop+User32.ShowWindow(IntPtr, SW)
   at System.Windows.Forms.Control.SetVisibleCore(Boolean)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean)
   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 repro_listview.Program.Main()

If it still doesn’t reproduce, you could try to increase the timeout in Thread.Sleep(), e.g. to 100 or 1000.

3reactions
RussKiecommented, Nov 23, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Implementing a custom sort for WinForms ListView
I was going to sort them using size, but not the displayed file name which is the item name in the listview ....
Read more >
ListView.ListViewItemSorter Property (System.Windows. ...
The ListViewItemSorter property allows you to specify the object that performs the sorting of items in a ListView control when the Sort method...
Read more >
Spotlight on the ListView Control – Part 3
The best solution is to use a ListViewItemSorter class. Unfortunately it requires you to create a custom C# class that will handle the...
Read more >
Fill a ListView with any Dataset, and perform sorting by a ...
This article will demonstrate how to perform custom sorting in the ... After the control is on the form, add items to the...
Read more >
What is ListView in C#?
C# ListView provides an interface to display a list of items using different kinds of views (e.g., text data, image data, and large...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found