XmlDocument.Save从GC线程抛出未处理的异常 [英] XmlDocument.Save unhandled exception thrown from GC thread

查看:103
本文介绍了XmlDocument.Save从GC线程抛出未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个简单的应用程序,当我尝试将XMLDocument保存到没有足够可用空间的拇指驱动器时,会发生奇怪的事情。虽然我已经为XMLDocument.Save(filePath)添加了try catch子句,但我仍然得到未处理的异常,并且异常似乎从GC终止线程抛出(似乎GC会尝试刷新磁盘没有的另一个4096字节)。我认为这是一个使用XMLDocument对象的常见场景,我不知道什么是错的?

代码片段:

  XmlDocument query = new XmlDocument(); 
query.Load(g:\\test.xml);
... //修改查询的内容以使其更大
try
{
query.Save(g:\\test.xml);

catch(Exception ex)
{}

异常stacktrace,如下所示:

lockquote
mscorlib.dll!System.IO .__ Error.WinIOError(int errorCode = 112,string maybeFullPath =)+ 0x498 bytes
mscorlib.dll!System.IO.FileStream.WriteCore(byte [] buffer,int offset,int count)+ 0x119 bytes
mscorlib.dll!System.IO.FileStream.FlushWrite(bool calledFromFinalizer )+ 0x22字节
mscorlib.dll!System.IO.FileStream.Dispose(bool disposing = false)+ 0x57字节

mscorlib.dll!System.IO.FileStream.Finalize()+ 0x1b字节



解决方案

好的,问题在于某些代码 当它完成时不会处理 FileStream 。当 FileStream 结束时,它会尝试刷新数据 - 此时抛出异常。



如果那真的是你所有的代码,它听起来像,它是 XmlDocument.Save()中的一个错误,它应该在它之前关闭所有的流返回。您可以通过自己打开 FileStream (使用语句使用)并将该流传递给 XmlDocument.Save


I develop a simple app, when i try to save an XMLDocument to a thumb drive which doesn't have enough free space, weird thing happens. Though i already added "try catch" clause for XMLDocument.Save(filePath), i still get an unhandled exception, and the exception seems to be thrown from GC finalization thread (seems GC tries to flush another 4096 bytes which the disk does not have). I think it is a common scenario to use XMLDocument object , i wonder what is wrong?

code snippet:

XmlDocument query = new XmlDocument();
query.Load("g:\\test.xml");
... //modify the content of query to make it bigger
try
{
  query.Save("g:\\test.xml");
}
catch(Exception ex)
{}

Exception stacktrace as below:

mscorlib.dll!System.IO.__Error.WinIOError(int errorCode = 112, string maybeFullPath = "") + 0x498 bytes mscorlib.dll!System.IO.FileStream.WriteCore(byte[] buffer, int offset, int count) + 0x119 bytes mscorlib.dll!System.IO.FileStream.FlushWrite(bool calledFromFinalizer) + 0x22 bytes mscorlib.dll!System.IO.FileStream.Dispose(bool disposing = false) + 0x57 bytes
mscorlib.dll!System.IO.FileStream.Finalize() + 0x1b bytes

解决方案

Well, the problem is that some code somewhere isn't disposing of the FileStream when it's done with it. When the FileStream is finalized, it's trying to flush the data - at which point the exception is being thrown.

If that really is all of your code, it sounds like it's a bug in XmlDocument.Save(), which should definitely close all its streams before returning. You could work around this by opening the FileStream yourself (with a using statement) and passing the stream to XmlDocument.Save instead.

这篇关于XmlDocument.Save从GC线程抛出未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆