YehudaKremer / msix

Create Msix installer for flutter windows-build files.
https://pub.dev/packages/msix
MIT License
279 stars 69 forks source link

[BUG] msvcp140.dll crashes and closes the app on windows 11 when playing sound #272

Closed Hamid313-coder closed 1 month ago

Hamid313-coder commented 3 months ago

When I build my flutter windows app which has "audio_players" and "video_player" on windows 11 using msix, it crashes and closes the app when I play sound or play video. I checked using "Event Viewer". it says the fault is from msvcp140.dll file. when I replace that .dll file with file which is located in "visual studio" folder. it works fine.

insertjokehere commented 3 months ago

This issue has eaten the last four days of my life, so bare with me if this explanation takes a bit.

  1. When building a VC++ app (like all Flutter apps) you need to link against the VC++ runtime, and you include a version of the libraries with your app when you distribute it to end users
  2. Microsoft requires that "the Redistributable version must be at least as new as the latest toolset used by any app component"
  3. This library includes an ancient version of the VC++ runtime, and bundles them into the generated installer:

https://github.com/YehudaKremer/msix/blob/2faab7e8de473fc8e30a0a2a92b3dcc6a06a65c7/lib/src/assets.dart#L263-L270

  1. Somehow, this has been working without major issues for the last few years - except that recently, Microsoft released an update to Visual Studio 2022 that fixed an issue in mutex from the VC++ runtime, but which requires that you follow the "at least as new as the latest toolset" rule. They even note in the changelog that

Programs that aren't following the documented restrictions on binary compatibility may encounter null dereferences in mutex machinery"

  1. Github have updated their windows and windows-latest runner images to use the newest Visual Studio, which has caused a bit of chaos generally: https://github.com/actions/runner-images/issues/10004

  2. The upshot of this is that if any of the Flutter plugins you are using use mutex, and you are building the app on a machine that has VS2022 17.10 or newer, and your users don't already have VC++ runtime 14.40.33810.0 or newer, your app will crash as soon as the plugin tries to use the mutex

As far as fixes are concerned, I'm not sure the best approach here. The current approach of bundling an static version of the VC++ redist with this module is fundamentally broken, but I acknowledge that we need some kind of fix to make it easier to bundle the required libraries with the installer.

I'm still working through a proper fix for my app, and will report back once I have one.

insertjokehere commented 3 months ago

OK - I have a workaround that Works For Me:

I'll work on getting a proper PR up and getting my package onto pub.dev - for now, you'll need to use the packages from git. Update your pubspec like:

dependencies:
  msvcredist:
    git:
      url: https://github.com/insertjokehere/flutter_msvcredist.git
      ref: main

dev_dependencies:
  msix:
    git:
      url: https://github.com/insertjokehere/msix.git
      ref: msvc_redist
PeterNjeim commented 3 months ago

Thank you for discovering the problem. I noticed that my local build worked fine but production build in GitHub Actions had the crash, and I analyzed the difference between the MSIX files and saw that they had a different DLL for a WebRTC plugin I use (which in an older version, immediately initiated an audio connection (still don't know why it gets a different DLL for it, but that's another issue)).

This made the app crash on startup, only in the production build lol. I also noticed that it had the old msvcp140.dll, vcruntime140.dll, and vcruntime140_1.dll. I noticed that replacing them with the new versions fixed it (or leaving them and instead changing the DLL of the WebRTC plugin). I was going to fix it by changing the DLL's manually, but I like the closure of knowing that it was audio (mutex) specifically that was causing the issue

UdaraAlwis commented 3 months ago

OK - I have a workaround that Works For Me:

I'll work on getting a proper PR up and getting my package onto pub.dev - for now, you'll need to use the packages from git. Update your pubspec like:

dependencies:
  msvcredist:
    git:
      url: https://github.com/insertjokehere/flutter_msvcredist.git
      ref: main

dev_dependencies:
  msix:
    git:
      url: https://github.com/insertjokehere/msix.git
      ref: msvc_redist

Thank you for posting this workaround! We had a similar issue on Windows 11 where our Flutter Windows app crashing whenever we accessed the webcam through the camera_windows library. We're also using this msix package for bundling our Windows app with flutter as well. This workaround helped us resolve that crash! I hope they release a permanent fix soon.

venky9885 commented 2 months ago

Error find from Event Viewer Faulting module name: MSVCP140.dll, version: 14.26.28804.1, time stamp: 0x5e9b29e5 Exception code: 0xc0000005

YehudaKremer commented 1 month ago

@riccardo-lomazzi fix it https://github.com/YehudaKremer/msix/pull/273 (version 3.16.8) sorry for the late response.