Rule to check that objects that implement IDIsposable are properly disposed not working (?)

csharp

(Catalin Ristache) #1

Version 6.4 (build 25310)
C# Sonar Way profile

Have a code like this:

public static void doSomething(string buzz)
{
    using (new LogFun(buzz))
    {
        var store = new typeImplementingIDisposable("My", StoreLocation.LocalMachine);
        try
        {
            store = new typeImplementingIDisposable("My", StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadWrite);
            //do something
        }
        finally
        {
            //store.Close();
        }
    }
}

Was wondering if there is a rule that would catch that I need to dispose the object and if it is why it didn’t catch it.
Thanks!


(Valeri Hristov) #2

We do have such rule, but it does not work for all types that implement IDisposable:
https://jira.sonarsource.com/browse/RSPEC-2930

The reason for limiting the rule is that not always the types that implement IDisposable should be disposed, for example Task is disposable, but should not be manually disposed. The .NET Framework is taking care of this.