开发者

How do I correctly dispose of an object? Why does Code Analysis keep changing its mind?

I joined a project yesterday, we decided to merge our two projects into one, and incorporate the features of both (they basically absorbed us).

The problem is, they have a memory leak somewhere, and they tasked me with finding and killing it.

I ran a Code Analysis in Visual Studio and came up with a long list of Warnings, most of which we can ignore, the ones I'm trying to fix are problems with object disposing. Something i've never really payed special attention to, and I'm surprised to find out that getting code analysis to decide that something is correct is not easy.

The code Started as this:

StreamWriter SW = new StreamWriter(File.Create("folder/file.txt"));
SW.WriteLine("text");
SW.WriteLine("text");
SW.WriteLine("text");
SW.WriteLine();
SW.WriteLine("text");
SW.WriteLine("text");
SW.WriteLine("text");
SW.Close();
SW.Flush();

But Code Analysis said that File.Create wasn't disposed of along all code paths and that SW can be disposed of more then once, I went looking and discovered that your not supposed to use both Close and Flush, and in fact, you should use a using statement, so i changed it to this:

using(StreamWriter SW = new StreamWriter(File.Create("folder/file.txt")))
{
    SW.WriteLine("text");
    SW.WriteLine("text");
    SW.WriteLine("text");
    SW.WriteLine();
    SW.WriteLine("text");
    SW.WriteLine("text");
    SW.WriteLine("text");
}

The SW being disposed went away, but it still said that File.Create wasn't being dispose开发者_如何学Cd of along all code paths, there is only one code path, so It didn't make sense, I tried adding a separate Using statement for File.Create, and Creating/writing the file separately, but code analysis still catches it.

This seems like something that would be easy to figure out, unless VS has a false positive or something.

Anyway, thanks for giving me the time to read this.

~EDIT

I lied in the origional post, when i add the double using statement, including when I use "Stream" it tells me the object can be disposed of more then once, causing an objectdisposedexception. I'm presuming that it would be in some weird freak occurrence. since I never dispose of it myself.

current code:

using (Stream stream = File.Create("folder/file.txt"))
{
    using (StreamWriter SW = new StreamWriter(stream))
    {
        SW.WriteLine("");
        SW.WriteLine("");
        SW.WriteLine("");
        SW.WriteLine();
        SW.WriteLine("");
        SW.WriteLine("");
        SW.WriteLine("");
    }
}

This one worked AND VS Passed it, thank you very much.

using (StreamWriter SW = File.CreateText("text/awardsList.txt"))
{
    SW.WriteLine("");
    SW.WriteLine("");
    SW.WriteLine("");
    SW.WriteLine();
}


I suspect it's looking for something like this:

using (Stream stream = File.Create("folder/file.txt"))
{
    using (StreamWriter writer = new StreamWriter(stream))
    {
        ...
    }
}

It's probably reasonable to assume that if File.Create succeeds, then so will the StreamWriter constructor - but it's possible that VS doesn't know that. It may also not know that disposing of the write disposes of the stream.

Personally I'd use File.CreateText instead though, which avoids the issue and is simpler :)

using (TextWriter writer = File.CreateText("folder/file.txt"))
{
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜