SEHException when disposing DeviceContext when using HardwareModeSwitch

This error came when I closed the game after running it in fullscreen mode with HardwareModeSwitch turned on. It’s a Windows/DirectX game.

System.Runtime.InteropServices.SEHException
ved SharpDX.Direct3D11.DeviceContext.Flush()
ved SharpDX.Direct3D11.DeviceContext.Dispose(Boolean disposing)
ved SharpDX.DisposeBase.CheckAndDispose(Boolean disposing)
ved SharpDX.Utilities.Dispose[T](T& comObject)
ved Microsoft.Xna.Framework.Graphics.GraphicsDevice.PlatformDispose() i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Graphics\GraphicsDevice.DirectX.cs:linje 991
ved Microsoft.Xna.Framework.Graphics.GraphicsDevice.Dispose(Boolean disposing) i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Graphics\GraphicsDevice.cs:linje 566
ved Microsoft.Xna.Framework.Graphics.GraphicsDevice.Dispose() i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Graphics\GraphicsDevice.cs:linje 522
ved Microsoft.Xna.Framework.GraphicsDeviceManager.Dispose(Boolean disposing) i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\GraphicsDeviceManager.cs:linje 239
ved Microsoft.Xna.Framework.GraphicsDeviceManager.Dispose() i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\GraphicsDeviceManager.cs:linje 227
ved Microsoft.Xna.Framework.Game.Dispose(Boolean disposing) i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Game.cs:linje 125
ved Microsoft.Xna.Framework.Game.Dispose() i C:\Users\User\Documents\Visual Studio 2017\Projects\MonoGame\MonoGame.Framework\Game.cs:linje 97

  private void PlatformDispose()
        {
            SharpDX.Utilities.Dispose(ref _renderTargetView);
            SharpDX.Utilities.Dispose(ref _depthStencilView);

            if (_userIndexBuffer16 != null)
                _userIndexBuffer16.Dispose();
            if (_userIndexBuffer32 != null)
                _userIndexBuffer32.Dispose();

            foreach (var vb in _userVertexBuffers.Values)
                vb.Dispose();

            SharpDX.Utilities.Dispose(ref _swapChain);

#if WINDOWS_STOREAPP || WINDOWS_UAP

            if (_bitmapTarget != null)
            {
                _bitmapTarget.Dispose();
                _depthStencilView = null;
            }
            if (_d2dDevice != null)
            {
                _d2dDevice.Dispose();
                _d2dDevice = null;
            }
            if (_d2dContext != null)
            {
                _d2dContext.Target = null;
                _d2dContext.Dispose();
                _d2dContext = null;
            }
            if (_d2dFactory != null)
            {
                _d2dFactory.Dispose();
                _d2dFactory = null;
            }
            if (_dwriteFactory != null)
            {
                _dwriteFactory.Dispose();
                _dwriteFactory = null;
            }
            if (_wicFactory != null)
            {
                _wicFactory.Dispose();
                _wicFactory = null;
            }

#endif // WINDOWS_STOREAPP

            SharpDX.Utilities.Dispose(ref _d3dContext); // <-- threw exception
            SharpDX.Utilities.Dispose(ref _d3dDevice);
        }

This exception happens every time.

Here is an issue on GitHub for SharpDX that appears to discuss the issue:

1 Like