dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.32k stars 958 forks source link

Unhandled exception has occurred when deleting ListView with setting its VirtualMode and VirtualListSize #11663

Closed Olina-Zhang closed 1 day ago

Olina-Zhang commented 3 weeks ago

.NET version

.NET 9.0.100-preview.7.24353.17

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Yes, it cannot repro in .NET 6.0 and 7.0, repro in .NET 8.0

Issue description

When deleting a ListView with setting its VirtualMode = true and custom a VirtualListSize value, unhandled exception pops up. image

_System.NullReferenceException: Object reference not set to an instance of an object. at System.Windows.Forms.ListView.ListViewNativeItemCollection.get_Item(Int32 displayIndex) in C:\Users\xxx\Documents\Winforms\src\System.Windows.Forms\src\System\Windows\Forms\Controls\ListView\ListView.ListViewNativeItemCollection.cs:line 46 at System.Windows.Forms.ListView.ListViewItemCollection.getItem(Int32 index) in C:\Users\xxx\Documents\Winforms\src\System.Windows.Forms\src\System\Windows\Forms\Controls\ListView\ListView.ListViewItemCollection.cs:line 65 at System.Windows.Forms.ListView.ReleaseUiaProvider(HWND handle) in C:\Users\xxx\Documents\Winforms\src\System.Windows.Forms\src\System\Windows\Forms\Controls\ListView\ListView.cs:line 5120 at System.Windows.Forms.Control.WmDestroy(Message& m) in C:\Users\xxx\Documents\Winforms\src\System.Windows.Forms\src\System\Windows\Forms\Control.cs:line 11607 at System.Windows.Forms.Control.WndProc(Message& m) in C:\Users\xxx\Documents\Winforms\src\System.Windows.Forms\src\System\Windows\Forms\Control.cs:line 12577 at System.Windows.Forms.ListView.WndProc(Message& m) in C:\Users\xxx\Documents\Winforms\src\System.Windows.Forms\src\System\Windows\Forms\Controls\ListView\ListView.cs:line 7137 at System.Windows.Forms.NativeWindow.Callback(HWND hWnd, MessageId msg, WPARAM wparam, LPARAM lparam) in C:\Users\xxx\Documents\Winforms\src\System.Windows.Forms\src\System\Windows\Forms\NativeWindow.cs:line 349

https://github.com/user-attachments/assets/4dcacb7f-09fe-4fd2-ab14-b59fb75bfc30

Steps to reproduce

Repro steps:

  1. Create a Winforms .NET application with ListView added
  2. Open ListView's properties window, set VirtualMode = true, set VirtualListSize = 2
  3. Delete ListView control
willibrandon commented 3 weeks ago

I believe this might have been introduced in #7478.

From the call stack:

System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.ListView.ListViewNativeItemCollection.get_Item(Int32 displayIndex)
   at System.Windows.Forms.ListView.ReleaseUiaProvider(HWND handle)
   at System.Windows.Forms.Control.WmDestroy(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ListView.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(HWND hWnd, MessageId msg, WPARAM wparam, LPARAM lparam)

ListView.ReleaseUiaProvider() iterates through the ListView's child elements by index and releases their UIA provider. https://github.com/dotnet/winforms/blob/1288baaad22ae0c73a91cc6f291d9d3e2baf97d3/src/System.Windows.Forms/src/System/Windows/Forms/Controls/ListView/ListView.cs#L5118-L5121

However, when in virtual mode, and with a VirtualListSize is set to a value greater than 0, the ListView's item count is set to the VirtualListSize. This causes ListView.ListViewNativeItemCollection.get_Item to reference null child elements when setting the child item index if the RetrieveVirtualItem event is not handled or returns null.