Sprog
Rediger

CA2000: Dispose objects before losing scope

Property Value
Rule ID CA2000
Title Dispose objects before losing scope
Category Reliability
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 10 No
Enabled by default in .NET 11 No
Applicable languages C# and Visual Basic

Cause

A local object of an IDisposable type is created, but the object is not disposed before all references to the object are out of scope.

By default, this rule analyzes the entire codebase, but this is configurable.

Rule description

If a disposable object is not explicitly disposed before all references to it are out of scope, the object will be disposed at some indeterminate time when the garbage collector runs the finalizer of the object. Because an exceptional event might occur that will prevent the finalizer of the object from running, the object should be explicitly disposed instead.

Special cases

Rule CA2000 does not fire for local objects of the following types even if the object is not disposed:

Passing an object of one of these types to a constructor and then assigning it to a field indicates a dispose ownership transfer to the newly constructed type. That is, the newly constructed type is now responsible for disposing of the object. If your code passes an object of one of these types to a constructor, no violation of rule CA2000 occurs even if the object is not disposed before all references to it are out of scope.

How to fix violations

To fix a violation of this rule, call Dispose on the object before all references to it are out of scope.

You can use the using statement (Using in Visual Basic) to wrap objects that implement IDisposable. Objects that are wrapped in this manner are automatically disposed at the end of the using block. However, the following situations should not or cannot be handled with a using statement:

  • To return a disposable object, the object must be constructed in a try/finally block outside of a using block.

  • Do not initialize members of a disposable object in the constructor of a using statement.

  • When constructors that are protected by only one exception handler are nested in the acquisition part of a using statement, a failure in the outer constructor can result in the object created by the nested constructor never being closed. In the following example, a failure in the StreamReader constructor can result in the FileStream object never being closed. CA2000 flags a violation of the rule in this case.

    using (StreamReader sr = new StreamReader(new FileStream("C:/myfile.txt", FileMode.Create)))
    { ... }
    
  • Dynamic objects should use a shadow object to implement the dispose pattern of IDisposable objects.

When to suppress warnings

Do not suppress a warning from this rule unless:

  • You've called a method on your object that calls Dispose, such as Close.
  • The method that raised the warning returns an IDisposable object that wraps your object.
  • The allocating method does not have dispose ownership; that is, the responsibility to dispose the object is transferred to another object or wrapper that's created in the method and returned to the caller.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA2000
// The code that's violating the rule is on this line.
#pragma warning restore CA2000

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA2000.severity = none

For more information, see How to suppress code analysis warnings.

Configure code to analyze

Use the following options to configure which parts of your codebase to run this rule on, and when dispose ownership should be transferred.

In addition, the following other dataflow analysis–related options apply to this rule:

You can configure these options for just this rule, for all rules they apply to, or for all rules in this category (Reliability) that they apply to. For more information, see Code quality rule configuration options.

Exclude specific symbols

You can exclude specific symbols, such as types and methods, from analysis by setting the excluded_symbol_names option. For example, to specify that the rule should not run on any code within types named MyType, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType

Note

Replace the XXXX part of CAXXXX with the ID of the applicable rule.

Allowed symbol name formats in the option value (separated by |):

  • Symbol name only (includes all symbols with the name, regardless of the containing type or namespace).
  • Fully qualified names in the symbol's documentation ID format. Each symbol name requires a symbol-kind prefix, such as M: for methods, T: for types, and N: for namespaces.
  • .ctor for constructors and .cctor for static constructors.

Examples:

Option Value Summary
dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType Matches all symbols named MyType.
dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType1|MyType2 Matches all symbols named either MyType1 or MyType2.
dotnet_code_quality.CAXXXX.excluded_symbol_names = M:NS.MyType.MyMethod(ParamType) Matches specific method MyMethod with the specified fully qualified signature.
dotnet_code_quality.CAXXXX.excluded_symbol_names = M:NS1.MyType1.MyMethod1(ParamType)|M:NS2.MyType2.MyMethod2(ParamType) Matches specific methods MyMethod1 and MyMethod2 with the respective fully qualified signatures.

Exclude specific types and their derived types

You can exclude specific types and their derived types from analysis by setting the excluded_type_names_with_derived_types option. For example, to specify that the rule should not run on any methods within types named MyType and their derived types, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType

Note

Replace the XXXX part of CAXXXX with the ID of the applicable rule.

Allowed symbol name formats in the option value (separated by |):

  • Type name only (includes all types with the name, regardless of the containing type or namespace).
  • Fully qualified names in the symbol's documentation ID format, with an optional T: prefix.

Examples:

Option value Summary
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType Matches all types named MyType and all of their derived types.
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType1|MyType2 Matches all types named either MyType1 or MyType2 and all of their derived types.
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = M:NS.MyType Matches specific type MyType with given fully qualified name and all of its derived types.
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = M:NS1.MyType1|M:NS2.MyType2 Matches specific types MyType1 and MyType2 with the respective fully qualified names, and all of their derived types.

Configure dispose ownership transfer

The dispose_ownership_transfer_at_constructor and dispose_ownership_transfer_at_method_call options configure the transfer of dispose ownership.

For example, to specify that the rule transfers dispose ownership for arguments passed to constructors, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.CAXXXX.dispose_ownership_transfer_at_constructor = true

Note

Replace the XXXX part of CAXXXX with the ID of the applicable rule.

dispose_ownership_transfer_at_constructor

Consider the following code example.

class A : IDisposable
{
    public void Dispose() { }
}

class Test
{
    DisposableOwnerType M1()
    {
        return new DisposableOwnerType(new A());
    }
}
  • If dotnet_code_quality.dispose_ownership_transfer_at_constructor is set to true, dispose ownership for the new A() allocation is transferred to the returned DisposableOwnerType instance.
  • If dotnet_code_quality.dispose_ownership_transfer_at_constructor is set to false, Test.M1() has the dispose ownership for new A(), and results in a CA2000 violation for a dispose leak.

dispose_ownership_transfer_at_method_call

Consider the following code example.

class Test
{
    void M1()
    {
        TransferDisposeOwnership(new A());
    }
}
  • If dotnet_code_quality.dispose_ownership_transfer_at_method_call is set to true, dispose ownership for the new A() allocation is transferred to the TransferDisposeOwnership method.
  • If dotnet_code_quality.dispose_ownership_transfer_at_method_call is set to false, Test.M1() has the dispose ownership for new A(), and results in a CA2000 violation for a dispose leak.

Example 1

Undisposed local object

The following example violates CA2000 because an FileStream is allocated locally but is not disposed before references go out of scope:

public void WriteLogViolation(string path, byte[] data)
{
    // CA2000 violation: FileStream is allocated but not disposed before leaving scope.
    FileStream stream = new FileStream(path, FileMode.OpenOrCreate);
    stream.Write(data, 0, data.Length);
}
Public Sub WriteLogViolation(ByVal path As String, ByVal data As Byte())
    ' CA2000 violation: FileStream is allocated but not disposed before leaving scope.
    Dim stream As New FileStream(path, FileMode.OpenOrCreate)
    stream.Write(data, 0, data.Length)
End Sub

To fix the violation, wrap the disposable object in a using statement or using declaration:

public void WriteLogFix(string path, byte[] data)
{
    // Fixed: stream is automatically disposed at the end of the method scope.
    using FileStream stream = new FileStream(path, FileMode.OpenOrCreate);
    stream.Write(data, 0, data.Length);
}
Public Sub WriteLogFix(ByVal path As String, ByVal data As Byte())
    ' Fixed: stream is automatically disposed at the end of the Using block.
    Using stream As New FileStream(path, FileMode.OpenOrCreate)
        stream.Write(data, 0, data.Length)
    End Using
End Sub

Factory method returning a disposable object

When implementing a method that creates and returns a disposable object, you cannot use a using block because the object would be disposed before the caller receives it. If intermediate operations between instantiation and return can throw an exception, wrap the initialization in a try/finally block to ensure the object is disposed if an exception occurs before ownership is transferred to the caller:

public FileStream CreateAndInitializeStream(string path)
{
    FileStream? tempStream = null;
    try
    {
        tempStream = new FileStream(path, FileMode.OpenOrCreate);

        // Write a file header. If this throws, tempStream is disposed in the finally block.
        byte[] header = Encoding.UTF8.GetBytes("LOG1");
        tempStream.Write(header, 0, header.Length);

        FileStream result = tempStream;
        tempStream = null; // Ownership transferred to caller; suppress disposal in finally.
        return result;
    }
    finally
    {
        tempStream?.Dispose();
    }
}
Public Function CreateAndInitializeStream(ByVal path As String) As FileStream
    Dim tempStream As FileStream = Nothing
    Try
        tempStream = New FileStream(path, FileMode.OpenOrCreate)

        ' Write a file header. If this throws, tempStream is disposed in the Finally block.
        Dim header As Byte() = Encoding.UTF8.GetBytes("LOG1")
        tempStream.Write(header, 0, header.Length)

        Dim result As FileStream = tempStream
        tempStream = Nothing ' Ownership transferred to caller; suppress disposal in Finally.
        Return result
    Finally
        If tempStream IsNot Nothing Then
            tempStream.Dispose()
        End If
    End Try
End Function

Example 2

By default, the Visual Basic compiler has all arithmetic operators check for overflow. Therefore, any Visual Basic arithmetic operation might throw an OverflowException. This could lead to unexpected violations in rules such as CA2000. For example, the following CreateReader1 function will produce a CA2000 violation because the Visual Basic compiler is emitting an overflow checking instruction for the addition that could throw an exception that would cause the StreamReader not to be disposed.

To fix this, you can disable the emitting of overflow checks by the Visual Basic compiler in your project or you can modify your code as in the following CreateReader2 function.

To disable the emitting of overflow checks, right-click the project name in Solution Explorer and then select Properties. Select Compile > Advanced Compile Options, and then check Remove integer overflow checks.

Imports System.IO

Class CA2000
    Public Function CreateReader1(ByVal x As Integer) As StreamReader
        Dim local As New StreamReader("C:\Temp.txt")
        x += 1
        Return local
    End Function


    Public Function CreateReader2(ByVal x As Integer) As StreamReader
        Dim local As StreamReader = Nothing
        Dim localTemp As StreamReader = Nothing
        Try
            localTemp = New StreamReader("C:\Temp.txt")
            x += 1
            local = localTemp
            localTemp = Nothing
        Finally
            If (Not (localTemp Is Nothing)) Then
                localTemp.Dispose()
            End If
        End Try
        Return local
    End Function
End Class

See also