Tag: objectdisposedexception

如何在应用新设置时阻止处理GraphicsDevice?

我的游戏窗口允许手动resize,这意味着它可以像任何其他普通窗口一样通过拖动其边缘来resize。 游戏还使用了RenderTarget2D rt2d ,主渲染目标在主Draw方法中设置: GraphicsDevice.SetRenderTarget(rt2d) ,但它在主要结尾处重置为null (默认渲染目标) Draw方法,这让它有点混乱:这真的是问题的根源,在Render Target设置为rt2d的时刻之前调整游戏窗口的大小,而不是重置回默认值? 现在它看起来像。 主Draw方法中的代码应始终将主Render Target重置为null ,因此没有预期的情况,通常不会发生这种情况。 尽管如此,调整游戏窗口大小的结果有时会导致GraphicsDevice.isDisposed返回true ,然后游戏会在第一个SpriteBatch.End()处抛出System.ObjectDisposedException 。 我发现这个错误的post可以追溯到XNA的第一天,但​​没有一个很好的解释(并且也没有提到更改渲染目标,所以它可能也是这些海报问题的根源)。 现在,我可以通过调用此方法几次来触发此错误: graphics.PreferredBackBufferWidth = graphics.PreferredBackBufferWidth; graphics.PreferredBackBufferHeight = graphics.PreferredBackBufferHeight; graphics.ApplyChanges(); …在Draw主方法中使用以下几行: RenderTarget2D rt2d = new RenderTarget2D(GraphicsDevice, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); GraphicsDevice.SetRenderTarget(rt2d); sb.Begin(); // main draw method here, it’s pretty big, so it might be taking long // enough to process to actually […]

清理方法中出现意外的“无法访问已处置的对象”

当我关闭我的WPF应用程序时,我正面临一个令人费解的处理对象问题。 如果你发现我的逻辑中有任何错误,请指出它们。 我有一个带有update()方法的ColorManager类,如下所示。 public void Update(ColorImageFrame frame) { byte[] pixelData = new byte[frame.PixelDataLength]; frame.CopyPixelDataTo(pixelData); if (Bitmap == null) { Bitmap = new WriteableBitmap(frame.Width, frame.Height, 96, 96, PixelFormats.Bgr32, null); } // draw bitmap RaisePropertyChanged(() => Bitmap); } 我在一个单独的线程中运行此方法。 在我的MainWindow.xaml.cs我有以下内容: private void Initialise() { if (kinectSensor == null) return; // start kinect sensor kinectSensor.Start(); updateColourStreamThread = new […]

如何在C#/ MVVM应用程序中解决无法解释的ObjectDisposedExceptions?

我编写了我的第一个MVVM应用程序。 当我关闭应用程序时,我常常因ObjectDisposedException而导致崩溃。 应用程序窗口消失后,应用程序即会崩溃。 获取堆栈跟踪很困难( 参见我的另一个问题 ),但最后我做了,发现我的堆栈跟踪完全包含在C#库中(kernel32!BaseThreadStart,mscorwks!Thread,mscorwks!WKS等)。 此外,这次崩溃是不一致的。 在我上次结账和重建之后,它停止了一段时间。 然后它又回来了。 一旦它开始发生,它就会不断发生, 即使我“清理”并重建 。 但擦拭和结账有时会使其停止一段时间。 我认为发生了什么: 我认为GarbageCollector在处理我的ViewModel时做的很有趣。 我的ViewModelBase类析构函数在调用析构函数时有一个WriteLine来记录,而在我的4个ViewModel中,只有2或3个被处理掉了,它似乎根据结帐而变化(例如,当我在我的运行时,我看到一直在重复顺序,但我的同事看到不同的序列与不同的对象处置)。 由于堆栈跟踪没有我的代码调用,我认为这意味着我的代码不是调用被处置对象的方法。 所以这让我觉得CLR是愚蠢的。 这有意义吗? 有什么方法可以让GC保持一致吗? 这是红鲱鱼吗? 其他可能有用的细节: 我的所有Views和ViewModel都是在App.xaml.cs文件的Application的Startup事件处理程序中创建的。 同一个处理程序将ViewModels分配给DataContexts。 我不确定这是否是正确的MVVM实践(正如我所说的,我的第一个MVVM应用程序),但我不明白为什么它会导致不良行为。 如有必要,我可以粘贴代码。

关闭.NET SerialPort后的ObjectDisposedExecption

我使用.NET 4 SerialPort对象与连接到COM1的设备进行通信。 当我完成设备后,我在SerialPort上调用Close。 我没有调用Dispose,但我相信Close和Dispose在这里是同义词。 通常这很好用。 但是,有时我会在一段时间后得到以下exception(我看过的时间范围从5毫秒到175毫秒): System.ObjectDisposedException:已关闭安全句柄 在System.Runtime.InteropServices.SafeHandle.DangerousAddRef(布尔和成功) 在System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle,布尔和成功) 在Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult(SafeFileHandle hFile,NativeOverlapped * lpOverlapped,Int32&lpNumberOfBytesTransferred,Boolean bWait) 在System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent() 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态) 在System.Threading.ThreadHelper.ThreadStart() 我的代码都没有在这个堆栈上。 我找到了http://blog.zachsaw.com/2010/07/serialport-ioexception-workaround-in-c.html ,但那里的解决方案没有用。 在进一步检查时,问题是IOException ,而不是ObjectDisposedException 。 关于拔掉USB到串口设备但是COM1在机上时观察到的问题,有很多post,所以它并没有意外消失。 这里的问题也不是我的问题; SerialPort在其使用期间保持活动状态,并且仅在我与设备通话时才关闭。 (一旦我完成,设备处于不会传输任何其他数据的状态。) SLaks 建议在SafeHandle.Dispose的入口处设置一个断点,以确定我何时处理我不应该的东西,但是我会破坏该断点数十次。 当我使用串行设备时,我单次调用SerialPort.Close调用了三次,其余大约一半都在GC线程中。 其余部分似乎与WPF UI元素相关。 我现在不知所措。 我从哪里去? 有没有办法确定哪个SafeHandle属于哪个对象,所以我可以肯定我没有意外地处理它? 除了Close之外还有一些咒语我需要正确关闭SerialPort吗?

谁拥有.NET中的包裹流(例如TextWriter)?

我最近遇到一个错误“ObjectDisposedException:无法访问已关闭的流” [ObjectDisposedException: Cannot access a closed Stream.] System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +10184402 System.Security.Cryptography.CryptoStream.FlushFinalBlock() +114 System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing) +48 使用以下格式的代码时: using (var stream = new MemoryStream()) { using (var hashStream = new CryptoStream(stream, new SHA256Managed(), CryptoStreamMode.Write)) using (var writer = new TextWriter(hashStream)) { writer.Write(“something”); } // ^– Exception occurs on hashStream Dispose // While naively […]

UdpClient.ReceiveAsync正确提前终止

美好的一天。 我使用UdpClient并使用它包装。 为了阅读我有异步方法: private async Task Receive(UdpClient client, CancellationToken breakToken) { // Выход из async, если произошёл CancellationRequest breakToken.ThrowIfCancellationRequested(); UdpReceiveResult result; try { result = await client.ReceiveAsync().WithCancellation(breakToken); } catch(OperationCanceledException) { // Штатная ситуация ручной остановки Task-а } return result.Buffer; } WithCancellation是我提前终止的扩展方法: public static async Task WithCancellation( this Task task, CancellationToken cancellationToken) { var tcs = […]

在.Net 2.0中关闭SerialPort时出现ObjectDisposedException

我有一个C#windows窗体应用程序,它通过COM端口与USB加密狗通信。 我正在使用.Net 2.0中的SerialPort类进行通信,并且串行端口对象在应用程序的生命周期内是打开的。 应用程序向设备发送命令,还可以从设备接收未经请求的数据。 表单关闭时出现问题 – 在尝试关闭COM端口时,我(随机地,不幸地)得到了ObjectDisposedException。 这是Windows堆栈跟踪: System.ObjectDisposedException was unhandled Message=Safe handle has been closed Source=System ObjectName=”” StackTrace: at Microsoft.Win32.UnsafeNativeMethods.SetCommMask(SafeFileHandle hFile, Int32 dwEvtMask) at System.IO.Ports.SerialStream.Dispose(Boolean disposing) at System.IO.Ports.SerialStream.Finalize() InnerException: 我找到了有类似问题的人的post,并尝试了解决方法[这里] [1] [1]:http: //zachsaw.blogspot.com/2010/07/net-serialport-woes.html虽然这是针对IOException并且没有阻止问题。 我的Close()代码如下: public void Close() { try { Console.WriteLine(“******ComPort.Close – baseStream.Close*******”); baseStream.Close(); } catch (Exception ex) { Console.WriteLine(“******ComPort.Close baseStream.Close raised exception: ” […]

CryptoStream可以让基本流打开吗?

我创建了一个MemoryStream ,将它传递给CryptoStream进行写入。 我希望CryptoStream加密,让MemoryStream对我开放,然后读入其他内容。 但是一旦处理掉CryptoStream ,它就会处理MemoryStream 。 CryptoStream可以以某种方式打开基本的MemoryStream吗? using (MemoryStream scratch = new MemoryStream()) { using (AesManaged aes = new AesManaged()) { // // Set some aes parameters, including Key, IV, etc. // ICryptoTransform encryptor = aes.CreateEncryptor(); using (CryptoStream myCryptoStream = new CryptoStream(scratch, encryptor, CryptoStreamMode.Write)) { myCryptoStream.Write(someByteArray, 0, someByteArray.Length); } } // Here, I’m still within […]

什么可能导致WCF中的“无法访问已处置的对象”错误?

我使用以下代码: private WSHttpBinding ws; private EndpointAddress Srv_Login_EndPoint; private ChannelFactory Srv_LoginChannelFactory; private Srv_Login.Srv_ILogin LoginService; Login是我的构造函数: public Login() { InitializeComponent(); ws = new WSHttpBinding(); Srv_Login_EndPoint = new EndpointAddress(“http://localhost:2687/Srv_Login.svc”); Srv_LoginChannelFactory = new ChannelFactory(ws, Srv_Login_EndPoint); } 我正在以这种方式使用服务: private void btnEnter_Click(object sender, EventArgs e) { try { LoginService = Srv_LoginChannelFactory.CreateChannel(); Srv_Login.LoginResult res = new Srv_Login.LoginResult(); res = LoginService.IsAuthenticated(txtUserName.Text.Trim(), txtPassword.Text.Trim()); if […]