Repository navigation
Open
Description
- .NET Core Version: 3.0.100-preview-009812
- Windows version: Windows 10.0.18323 19H1
- Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
Problem description:
Necessary conditions:
- Create multiple WPF UI threads
- In fact, two are enough, one is the main UI thread with the App class we usually write; a background UI thread, for example, to display the UI thread that starts the splash screen.
- If you use two threads, you need a lot of repetitive trials to reproduce; and by creating more threads you can greatly improve the probability of a single recurrence
- These UI threads all display WPF windows
- This issue will occur in both WPF on .NET Core 3 and WPF on .NET Framework 4.8.
phenomenon:
- An exception is thrown and the application crashes
Actual behavior:
Exception thrown: 'System.NullReferenceException' in WindowsBase.dll
Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at System.IO.Packaging.PackagePart.CleanUpRequestedStreamsList()
at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at Walterlv.Bugs.MultiThreadedUI.SplashWindow.InitializeComponent() in C:\Users\lvyi\Desktop\Walterlv.Bugs.MultiThreadedUI\Walterlv.Bugs.MultiThreadedUI\SplashWindow.xaml:line 1
at Walterlv.Bugs.MultiThreadedUI.SplashWindow..ctor() in C:\Users\lvyi\Desktop\Walterlv.Bugs.MultiThreadedUI\Walterlv.Bugs.MultiThreadedUI\SplashWindow.xaml.cs:line 24
at Walterlv.Bugs.MultiThreadedUI.Program.<>c__DisplayClass1_0.<RunSplashWindow>b__0() in C:\Users\lvyi\Desktop\Walterlv.Bugs.MultiThreadedUI\Walterlv.Bugs.MultiThreadedUI\Program.cs:line 33
Expected behavior:
Don't crash.
Minimal repro:
- Create a new WPF project (either .NET Core 3 or .NET Framework 4.8)
- Keep the automatically generated
AppandMainWindowunchanged, we create a new windowSplashWindow. - Create a new
Programclass containing the Main function and setProgramas the startup object (instead ofApp) in the project properties.
All other files remain the same as the default code generated by Visual Studio, and the code of Program.cs is as follows:
using System;
using System.Threading;
using System.Windows.Threading;
namespace Walterlv.Bugs.MultiThreadedUI
{
public class Program
{
[STAThread]
private static void Main(string[] args)
{
for (var i = 0; i < 50; i++)
{
RunSplashWindow(i);
}
var app = new App();
app.InitializeComponent();
app.Run();
}
private static void RunSplashWindow(int index)
{
var thread = new Thread(() =>
{
var window = new SplashWindow
{
Title = $"SplashWindow {index.ToString().PadLeft(2, ' ')}",
};
window.Show();
Dispatcher.Run();
})
{
IsBackground = true,
};
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
}
}Remarks: Even if you add this code just before the Splash Window creating, this exception still occurs.
SynchronizationContext.SetSynchronizationContext(
new DispatcherSynchronizationContext(
Dispatcher.CurrentDispatcher));Reacted by lindexi, Matthew Thomas, Acoris, UlrichGit, Rao Hammas and sra-erniesalazar
Metadata
Metadata
Assignees
Labels
BugProduct bug (most likely)


Activity
walterlv commentedon Jan 29, 2019
The null object may be
_requestedStreamsand this is the code that set it to null:walterlv commentedon Jan 29, 2019
The same issue can be seen here:
Because I can't find out why this bug happens, so I just post the issue itself.
stevenbrix commentedon Feb 14, 2019
Thanks for filing this issue @walterlv. This item and #298 seem very similar, is the reason you opened a separate issue because the crashes have different callstacks?
walterlv commentedon Feb 14, 2019
@stevenbrix The two issues have something differences:
If it's recommended to merge these two issues, I'll do that.
stevenbrix commentedon Feb 15, 2019
Thanks @walterlv. I just wanted to make sure that the repos and symptoms were indeed different. I think we should keep these two separate considering they will have separate fixes.
walterlv commentedon Feb 19, 2019
@stevenbrix I've posted the code to GitHub:
The
Walterlv.Bugs.MultiThreadedUIis the .NET Framework version and theWalterlv.Bugs.MultiThreadedUI.Coreis the .NET Core version.FlsZen commentedon Sep 13, 2019
We're also running into this behavior with our .NET Framework 4.7.2 app. In our case, we start up another thread to render reports on, and will sometimes get the
NullReferenceExceptionmentioned here, and other times get aIndexOutOfRangeExceptioninPackagePart.CleanUpRequestedStreamsList(). It seems that the multiple threads wind up working on the same PackagePart.angtianqiang commentedon Sep 29, 2020
I also encountered such a problem. How did you solve it
matthew-a-thomas commentedon Sep 29, 2020
The root cause is apparently a race condition within the automatically generated
InitializeComponent()method.So the way I've worked around this is to call
InitializeComponent()within a lock on a static singleton. For example:Note: you can add code-behind for
ResourceDictionarys too. The code-behind gives you a place to add thelockstatement if needed:I've used the above in production code to work around this issue. Perhaps one would be able to find a more elegant solution by studying the source code behind the offending framework call to
System.IO.Packaging.PackagePart.CleanUpRequestedStreamsList(), but I don't like spending my time trying to work around decades-old Microsoft bugs so I did the above and moved on.merge: CI stability — serialize settings/WPF suites, bound STA joins,…