内存异常XDocument.Save() [英] Memory exception while XDocument.Save()

查看:215
本文介绍了内存异常XDocument.Save()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将XDcoument保存到没有足够内存空间的拇指驱动器。 (这是应用程序的特殊测试条件)虽然应用程序正在提供如下所示的异常,但我无法在XDocument.Save(filePath)周围的try catch块中找到它。看起来像是延期投掷。是LINQ还是我做错了?

I am trying to save an XDcoument to a thumb drive which doesnt have enough memory space available. (This is a special test condition for the app) Though the application is giving an exception like below, I cant get that in the try catch block around the XDocument.Save(filePath). Looks like it is a delayed throw. Is it a LINQ issue or am I doing something wrong?.

alt text http://img211.imageshack.us/img211/8324/exce.png

 System.IO.IOException was unhandled
 Message="There is not enough space on the disk.\r\n"
 Source="mscorlib"
 StackTrace:
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.FileStream.FlushWrite(Boolean calledFromFinalizer)
   at System.IO.FileStream.Dispose(Boolean disposing)
   at System.IO.FileStream.Finalize()


推荐答案

您在框架中发现了一个错误。 XDocument.Save(string)使用using语句来确保输出流被处理。这取决于您在处理指令中使用的编码,但内部的System.Xml.XmlUtf8RawTextReader将是实现文本编写器的常见编码器。

You found a bug in the framework. XDocument.Save(string) uses the "using" statement to ensure the output stream gets disposed. It depends on the encoding you used in the processing instruction but the internal System.Xml.XmlUtf8RawTextReader would be a common one to implement the text writer.

错误:Microsoft编写该类的程序员忘了实现Dispose()方法。只有Close()方法被实现。

The bug: the Microsoft programmer that wrote that class forgot to implement the Dispose() method. Only the Close() method is implemented.

很奇怪,这个错误还没有在connect.microsoft.com反馈站点报告。它应该导致一般使用的麻烦,因为文件保持打开,直到定稿器线程运行。虽然通常不需要很长时间,几秒钟左右。除非您在写入后立即退出程序,否则在缓冲区被刷新的确切时刻,不幸的是运行磁盘空间。

It is rather strange that this bug wasn't yet reported at the connect.microsoft.com feedback site. It ought to cause trouble in general use because the file stays open until the finalizer thread runs. Although that normally doesn't take that long, a couple of seconds or so. Except in your case where you exit the program right after writing and have the unfortunate luck to run out of disk space at the exact moment the buffer gets flushed.

这个错误是使用XDocument.Save(TextWriter)重载,传递一个StreamWriter,其编码与XML的编码相匹配。

A workaround for this bug is to use the XDocument.Save(TextWriter) overload instead, passing a StreamWriter whose Encoding matches the encoding of the XML.

这篇关于内存异常XDocument.Save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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