Skip to content

Commit a56689b

Browse files
committed
update ca2213
1 parent 5786b1e commit a56689b

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

docs/code-quality/ca2213-disposable-fields-should-be-disposed.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "CA2213: Disposable fields should be disposed"
3-
ms.date: 11/04/2016
3+
ms.date: 11/05/2018
44
ms.prod: visual-studio-dev15
55
ms.technology: vs-ide-code-analysis
66
ms.topic: reference
@@ -27,26 +27,33 @@ ms.workload:
2727
|Breaking Change|Non Breaking|
2828

2929
## Cause
30-
A type that implements <xref:System.IDisposable?displayProperty=fullName> declares fields that are of types that also implement <xref:System.IDisposable>. The <xref:System.IDisposable.Dispose%2A> method of the field is not called by the <xref:System.IDisposable.Dispose%2A> method of the declaring type.
30+
31+
A type that implements <xref:System.IDisposable?displayProperty=fullName> declares fields that are of types that also implement <xref:System.IDisposable>. The <xref:System.IDisposable.Dispose%2A> method of the field is not called by the <xref:System.IDisposable.Dispose%2A> method of the declaring type.
3132

3233
## Rule description
33-
A type is responsible for disposing of all its unmanaged resources; this is accomplished by implementing <xref:System.IDisposable>. This rule checks to see whether a disposable type `T` declares a field `F` that is an instance of a disposable type `FT`. For each field `F`, the rule attempts to locate a call to `FT.Dispose`. The rule searches the methods called by `T.Dispose`, and one level lower (the methods called by the methods called by `FT.Dispose`).
34+
35+
A type is responsible for disposing of all its unmanaged resources. Rule CA2213 checks to see whether a disposable type (that is, one that implements <xref:System.IDisposable>) `T` declares a field `F` that is an instance of a disposable type `FT`. For each field `F` that's assigned a locally created object within the methods or initializers of the containing type `T`, the rule attempts to locate a call to `FT.Dispose`. The rule searches the methods called by `T.Dispose` and one level lower (that is, the methods called by the methods called by `FT.Dispose`).
36+
37+
> [!NOTE]
38+
> Rule CA2213 fires only for fields that are assigned a locally created disposable object within the containing type's methods and initializers. If the object is created or assigned outside of type `T`, the rule does not fire. This reduces noise for cases where the containing type doesn't own the responsibility for disposing of the object.
3439
3540
## How to fix violations
36-
To fix a violation of this rule, call <xref:System.IDisposable.Dispose%2A> on fields that are of types that implement <xref:System.IDisposable> if you are responsible for allocating and releasing the unmanaged resources held by the field.
41+
42+
To fix a violation of this rule, call <xref:System.IDisposable.Dispose%2A> on fields that are of types that implement <xref:System.IDisposable>.
3743

3844
## When to suppress warnings
39-
It is safe to suppress a warning from this rule if you are not responsible for releasing the resource held by the field, or if the call to <xref:System.IDisposable.Dispose%2A> occurs at a deeper calling level than the rule checks.
45+
46+
It is safe to suppress a warning from this rule if you're not responsible for releasing the resource held by the field, or if the call to <xref:System.IDisposable.Dispose%2A> occurs at a deeper calling level than the rule checks.
4047

4148
## Example
42-
The following example shows a type `TypeA` that implements <xref:System.IDisposable> (`FT` in the previosu discussion).
4349

44-
[!code-csharp[FxCop.Usage.IDisposablePattern#1](../code-quality/codesnippet/CSharp/ca2213-disposable-fields-should-be-disposed_1.cs)]
50+
The following snippet shows a type `TypeA` that implements <xref:System.IDisposable>.
4551

46-
## Example
47-
The following example shows a type `TypeB` that violates this rule by declaring a field `aFieldOfADisposableType` (`F` in the previous discussion) as a disposable type (`TypeA`) and not calling <xref:System.IDisposable.Dispose%2A> on the field. `TypeB` corresponds to `T` in the previous discussion.
52+
[!code-csharp[FxCop.Usage.IDisposablePattern#1](../code-quality/codesnippet/CSharp/ca2213-disposable-fields-should-be-disposed_1.cs)]
53+
54+
The following snippet shows a type `TypeB` that violates rule CA2213 by declaring a field `aFieldOfADisposableType` as a disposable type (`TypeA`) and not calling <xref:System.IDisposable.Dispose%2A> on the field.
4855

49-
[!code-csharp[FxCop.Usage.IDisposableFields#1](../code-quality/codesnippet/CSharp/ca2213-disposable-fields-should-be-disposed_2.cs)]
56+
[!code-csharp[FxCop.Usage.IDisposableFields#1](../code-quality/codesnippet/CSharp/ca2213-disposable-fields-should-be-disposed_2.cs)]
5057

5158
## See also
5259

docs/code-quality/codesnippet/CSharp/ca2213-disposable-fields-should-be-disposed_1.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using System;
1+
using System;
22

33
namespace UsageLibrary
44
{
5-
public class TypeA :IDisposable
5+
public class TypeA : IDisposable
66
{
7-
8-
protected virtual void Dispose(bool disposing)
7+
protected virtual void Dispose(bool disposing)
98
{
10-
if (disposing)
9+
if (disposing)
1110
{
1211
// Dispose managed resources
1312
}
@@ -17,11 +16,8 @@ protected virtual void Dispose(bool disposing)
1716

1817
public void Dispose()
1918
{
20-
21-
Dispose(true);
22-
23-
GC.SuppressFinalize(this);
24-
19+
Dispose(true);
20+
GC.SuppressFinalize(this);
2521
}
2622

2723
// Disposable types implement a finalizer.

docs/code-quality/codesnippet/CSharp/ca2213-disposable-fields-should-be-disposed_2.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace UsageLibrary
44
{
@@ -8,21 +8,21 @@ public class TypeB : IDisposable
88
TypeA aFieldOfADisposableType = new TypeA();
99
private bool disposed = false;
1010

11-
protected virtual void Dispose(bool disposing)
11+
protected virtual void Dispose(bool disposing)
1212
{
13-
if (!disposed)
13+
if (!disposed)
1414
{
1515
// Dispose of resources held by this instance.
1616

1717
// Violates rule: DisposableFieldsShouldBeDisposed.
1818
// Should call aFieldOfADisposableType.Dispose();
1919

2020
disposed = true;
21-
// Suppress finalization of this disposed instance.
22-
if (disposing)
23-
{
24-
GC.SuppressFinalize(this);
25-
}
21+
// Suppress finalization of this disposed instance.
22+
if (disposing)
23+
{
24+
GC.SuppressFinalize(this);
25+
}
2626
}
2727
}
2828

0 commit comments

Comments
 (0)