dotnet / dotnet/wpf

WindowCollection RemoveAt does not correctly remove window

Open
#11,610 2 comments 0 reactions 1 assignee Claimed by @h3xds1nz View on GitHub
Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

I discovered this bug while exploring an unrelated "unconventional use case" for a WPF application. Without getting into too much detail, I need the Dispatcher for my application to fully "wind down" on application shutdown, allowing a new Dispatcher to later be started on the same thread. I'm aware this is not an "officially supported" use case, but it led me to find an actual unrelated bug which prevents the use case from working when it otherwise should.

When `Application` shuts down, it queues `ShutdownCallback` on the Dispatcher which eventually calls `Application.DoShutdown`. The DoShutdown method tries to close and remove all application windows in a loop:
https://github.com/dotnet/wpf/blob/07dc7ddaab3b30de79c140c679decba77c16f82a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs#L1594-L1605

However, the loop never exits because the windows are never actually removed from the collection. It seems that `WindowCollection.RemoveAt` uses the wrong Remove method on ArrayList:

https://github.com/dotnet/wpf/blob/07dc7ddaab3b30de79c140c679decba77c16f82a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/WindowCollection.cs#L166-L172

The `Remove` method on ArrayList expects a reference to an item that is in the list, not a list index:

Image

This means that it unsuccessfully tries to remove the item `(object) 0` from a list of Window objects, which obviously never succeeds. The `WindowCollection.RemoveAt` method _should_ be calling `_list.RemoveAt(index)`.

I'm _assuming_ (more like guessing) that this logic is supposed to perform "last resort clean-up" that is not normally encountered for normal WPF applications, as the windows likely get closed (and possibly removed from the collection) through a different code path during "clean shutdown".

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.