凡在我的代码被泄漏? [英] where is leak in my code?

查看:107
本文介绍了凡在我的代码被泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码,打开一个XML文件(old.xml),过滤无效字符,并写入到另一个XML文件(abc.xml)。最后,我将再次加载XML(abc.xml)。执行followling线的时候,有异常说,XML文件由另一个进程使用,

Here is my code which opens an XML file (old.xml), filter invalid characters and write to another XML file (abc.xml). Finally I will load the XML (abc.xml) again. When executing the followling line, there is exception says the xml file is used by another process,

xDoc.Load("C:\\abc.xml");



有没有人有任何想法有什么不好?在我的代码,为什么任何泄漏(我用的使用关键字的时候,迷茫地看到泄漏......)?

Does anyone have any ideas what is wrong? Any leaks in my code and why (I am using "using" keyword all the time, confused to see leaks...)?

下面是我的全部代码,我我使用C#+ VSTS 2008在Windows Vista x64的。

Here is my whole code, I am using C# + VSTS 2008 under Windows Vista x64.

    // Create an instance of StreamReader to read from a file.
    // The using statement also closes the StreamReader.
    Encoding encoding = Encoding.GetEncoding("utf-8", new EncoderReplacementFallback(String.Empty), new DecoderReplacementFallback(String.Empty));
    using (TextWriter writer = new StreamWriter(new FileStream("C:\\abc.xml", FileMode.Create), Encoding.UTF8))
    {
        using (StreamReader sr = new StreamReader(
            "C:\\old.xml",
            encoding
            ))
        {
            int bufferSize = 10 * 1024 * 1024; //could be anything
            char[] buffer = new char[bufferSize];
            // Read from the file until the end of the file is reached.
            int actualsize = sr.Read(buffer, 0, bufferSize);
            writer.Write(buffer, 0, actualsize);
            while (actualsize > 0)
            {
                actualsize = sr.Read(buffer, 0, bufferSize);
                writer.Write(buffer, 0, actualsize);
            }
        }
    }

    try
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load("C:\\abc.xml");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }



EDIT1:我试图缓冲区的大小从10M变更为1M和有用!我很困惑,任何想法

I have tried to change the size of buffer from 10M to 1M and it works! I am so confused, any ideas?

EDIT2:我觉得这个问题很容易重现时,输入旧的XML文件是非常大的,像100M或东西。我怀疑它是否是一个.NET已知的bug?我将使用诸如ProcessExplorer /工具的ProcessMonitor,看看哪个进程锁定文件,以保持它由XMLDocument.load方法被访问。

I find this issue is very easy to reproduce when the input old XML file is very big, like 100M or something. I am suspecting whether it is a .Net known bug? I am going to using tools like ProcessExplorer/ProcessMonitor to see which process locks the file to keep it from being accessed by XmlDocument.Load.

推荐答案

这对我来说工作正常。
纯粹是猜测,但也许一个病毒检查程序扫描文件?
探讨,尝试禁用病毒检查程序,看看它的工作原理(然后重新启用病毒检查程序)。

That works fine for me. Purely a guess, but maybe a virus checker is scanning the file? To investigate, try disabling your virus checker and see if it works (and then re-enable your virus checker).

顺便说一句,有一个方式,它的可以的保留文件打开:如果的StreamReader 构造函数抛出异常;但你不会达到的XmlDocument 的东西反正...但考虑到:

As an aside, there is one way it can leave the file open: if the StreamReader constructor throws an exception; but then you won't reach the XmlDocument stuff anyway... but consider:

using (FileStream fs = new FileStream("C:\\abc.xml", FileMode.Create))
using (TextWriter writer = new StreamWriter(fs, Encoding.UTF8))
{
   ...
}

现在 FS 设置在边缘情况下新的StreamWriter(...)抛出。不过,我的不可以认为,这是这里的问题。

Now fs is disposed in the edge-case where new StreamWriter(...) throws. However, I do not believe that this is the problem here.

这篇关于凡在我的代码被泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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