[Fortify][.NET]Unreleased Resource: Streams 排除

Obviously with a colleague using to make sure that at the end of the block will call Dispose () done automatically release the resources, but I got the source code inspection tools fortify report. Call ~ ~ to solve problems.


As follows, Developer good as gold have to use custom objects using the range at the end to make calls Dispose using block () done automatically release resources

using (FileStream fsInFile = new FileStream(@"C:Testfile.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
    using (StreamReader srInFile = new StreamReader(fsInFile, System.Text.Encoding.Default))
    {
        string strInfData;
        while ((strInfData = srInFile.ReadLine()) != null)
        {
            throw new Exception("somebody else"); //故意直接跳出method
        }
    }
}

In addition, Microsoft docs for using declarative description:

Providing convenient syntax to ensure the proper use of IDisposable objects.

But it was swept out Unreleased Resource: Streams

Checked using Microsoft docs in the description, there is a line to attract the eye, she said, even if the document Exception occurred, you can also ensure that executes Dispose

The using statement ensures that Dispose is called even if an exception occurs within the using block.

It seems to fortify misjudged.


Correction

Old trick, continues Catch Me If You Can, then we add in a row before the accident Dispose ()

using (FileStream fsInFile = new FileStream(@"C:Testfile.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
    using (StreamReader srInFile = new StreamReader(fsInFile, System.Text.Encoding.Default))
    {
        string strInfData;
        while ((strInfData = srInFile.ReadLine()) != null)
        {
            fsInFile.Dispose();
            throw new Exception("somebody else");
        }
    }
}

Rescan:

Closed!


Experimental deconstruction whether to perform a function using the block jump occurred Exception

In addition to watching Microsoft Docs document, we also do scientific experiments.

We have a player class: Footballer, New Atletico Madrid striker grizmann up after a shot execution, we deliberately enter Exception, to see if the deconstruction letter shows will perform?

using System;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("程序开始");
            Attack();
            Console.WriteLine("程序结束");
        }

        private static void Attack()
        {
            using (Footballer griezmann = new Footballer())
            {
                try
                {
                    griezmann.Shot();
                    throw new Exception("Error");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return;
                }
            }
        }
    }

    internal class Footballer : IDisposable
    {
        public Footballer()
        {
            Console.WriteLine("建构函数");
        }

        public void Shot()
        {
            Console.WriteLine("射门!!!");
        }

        public void Dispose()
        {
            Console.WriteLine("解构函数");
            GC.SuppressFinalize(this);
        }
    }
}

Executive deconstruction! Resources will be released ~

Semi Finals

4 expect strong, less Spain.


reference

Microsoft docs using statement

https://docs.microsoft.com/zh-tw/dotnet/csharp/language-reference/keywords/using-statement

原文:大专栏  [Fortify][.NET]Unreleased Resource: Streams 排除


Guess you like

Origin www.cnblogs.com/petewell/p/11444886.html