Skip to content

Commit 10fa74a

Browse files
authored
Merge pull request #2408 from gewarren/CA2
Clarify CA1816 examples
2 parents 67a01e4 + c76faa9 commit 10fa74a

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed
Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "CA1816: Call GC.SuppressFinalize correctly"
3-
ms.date: 11/04/2016
3+
ms.date: 06/30/2018
44
ms.prod: visual-studio-dev15
55
ms.technology: vs-ide-code-analysis
66
ms.topic: reference
@@ -14,10 +14,14 @@ ms.assetid: 47915fbb-103f-4333-b157-1da16bf49660
1414
author: gewarren
1515
ms.author: gewarren
1616
manager: douge
17+
dev_langs:
18+
- CSharp
19+
- VB
1720
ms.workload:
1821
- "multiple"
1922
---
2023
# CA1816: Call GC.SuppressFinalize correctly
24+
2125
|||
2226
|-|-|
2327
|TypeName|CallGCSuppressFinalizeCorrectly|
@@ -27,45 +31,53 @@ ms.workload:
2731

2832
## Cause
2933

30-
- A method that is an implementation of <xref:System.IDisposable.Dispose%2A?displayProperty=fullName> does not call <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>.
34+
Violations of this rule can be caused by:
35+
36+
- A method that is an implementation of <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> and doesn't call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>.
37+
38+
- A method that is not an implementation of <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> and calls <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>.
39+
40+
- A method that calls <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> and passes something other than [this (C#)](/dotnet/csharp/language-reference/keywords/this) or [Me (Visual Basic)](/dotnet/visual-basic/programming-guide/program-structure/me-my-mybase-and-myclass#me).
41+
42+
## Rule description
43+
44+
The <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method lets users release resources at any time before the object becoming available for garbage collection. If the <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> method is called, it frees resources of the object. This makes finalization unnecessary. <xref:System.IDisposable.Dispose%2A?displayProperty=nameWithType> should call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> so the garbage collector doesn't call the finalizer of the object.
45+
46+
To prevent derived types with finalizers from having to reimplement <xref:System.IDisposable> and to call it, unsealed types without finalizers should still call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>.
47+
48+
## How to fix violations
3149

32-
- A method that is not an implementation of <xref:System.IDisposable.Dispose%2A?displayProperty=fullName> calls <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>.
50+
To fix a violation of this rule:
3351

34-
- A method calls <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName> and passes something other than this (Me in Visual Basic).
52+
- If the method is an implementation of <xref:System.IDisposable.Dispose%2A>, add a call to <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>.
3553

36-
## Rule Description
37-
The <xref:System.IDisposable.Dispose%2A?displayProperty=fullName> method lets users release resources at any time before the object becoming available for garbage collection. If the <xref:System.IDisposable.Dispose%2A?displayProperty=fullName> method is called, it frees resources of the object. This makes finalization unnecessary. <xref:System.IDisposable.Dispose%2A?displayProperty=fullName> should call <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName> so the garbage collector does not call the finalizer of the object.
54+
- If the method is not an implementation of <xref:System.IDisposable.Dispose%2A>, either remove the call to <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> or move it to the type's <xref:System.IDisposable.Dispose%2A> implementation.
3855

39-
To prevent derived types with finalizers from having to re-implement <xref:System.IDisposable> and to call it, unsealed types without finalizers should still call <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>.
56+
- Change all calls to <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> to pass [this (C#)](/dotnet/csharp/language-reference/keywords/this) or [Me (Visual Basic)](/dotnet/visual-basic/programming-guide/program-structure/me-my-mybase-and-myclass#me).
4057

41-
## How to Fix Violations
42-
To fix a violation of this rule:
58+
## When to suppress warnings
4359

44-
If the method is an implementation of <xref:System.IDisposable.Dispose%2A>, add a call to <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>.
60+
Only suppress a warning from this rule if you are deliberately using <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> to control the lifetime of other objects. Don't suppress a warning from this rule if an implementation of <xref:System.IDisposable.Dispose%2A> doesn't call <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>. In this situation, failing to suppress finalization degrades performance and provides no benefits.
4561

46-
If the method is not an implementation of <xref:System.IDisposable.Dispose%2A>, either remove the call to <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName> or move it to the type's <xref:System.IDisposable.Dispose%2A> implementation.
62+
## Example that violates CA1816
4763

48-
Change all calls to <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName> to pass this (Me in Visual Basic).
64+
This code shows a method that calls <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType>, but doesn't pass [this (C#)](/dotnet/csharp/language-reference/keywords/this) or [Me (Visual Basic)](/dotnet/visual-basic/programming-guide/program-structure/me-my-mybase-and-myclass#me). As a result, this code violates rule CA1816.
4965

50-
## When to Suppress Warnings
51-
Only suppress a warning from this rule if you are deliberating using <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName> to control the lifetime of other objects. Do not suppress a warning from this rule if an implementation of <xref:System.IDisposable.Dispose%2A> does not call <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>. In this situation, failing to suppress finalization degrades performance and provide no benefits.
66+
[!code-vb[FxCop.Usage.CallGCSuppressFinalizeCorrectly#1](../code-quality/codesnippet/VisualBasic/ca1816-call-gc-suppressfinalize-correctly_1.vb)]
67+
[!code-csharp[FxCop.Usage.CallGCSuppressFinalizeCorrectly#1](../code-quality/codesnippet/CSharp/ca1816-call-gc-suppressfinalize-correctly_1.cs)]
5268

53-
## Example
54-
The following example shows a method that incorrectly calls <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>.
69+
## Example that satisfies CA1816
5570

56-
[!code-vb[FxCop.Usage.CallGCSuppressFinalizeCorrectly#1](../code-quality/codesnippet/VisualBasic/ca1816-call-gc-suppressfinalize-correctly_1.vb)]
57-
[!code-csharp[FxCop.Usage.CallGCSuppressFinalizeCorrectly#1](../code-quality/codesnippet/CSharp/ca1816-call-gc-suppressfinalize-correctly_1.cs)]
71+
This example shows a method that correctly calls <xref:System.GC.SuppressFinalize%2A?displayProperty=nameWithType> by passing [this (C#)](/dotnet/csharp/language-reference/keywords/this) or [Me (Visual Basic)](/dotnet/visual-basic/programming-guide/program-structure/me-my-mybase-and-myclass#me).
5872

59-
## Example
60-
The following example shows a method that correctly calls <xref:System.GC.SuppressFinalize%2A?displayProperty=fullName>.
73+
[!code-vb[FxCop.Usage.CallGCSuppressFinalizeCorrectly2#1](../code-quality/codesnippet/VisualBasic/ca1816-call-gc-suppressfinalize-correctly_2.vb)]
74+
[!code-csharp[FxCop.Usage.CallGCSuppressFinalizeCorrectly2#1](../code-quality/codesnippet/CSharp/ca1816-call-gc-suppressfinalize-correctly_2.cs)]
6175

62-
[!code-vb[FxCop.Usage.CallGCSuppressFinalizeCorrectly2#1](../code-quality/codesnippet/VisualBasic/ca1816-call-gc-suppressfinalize-correctly_2.vb)]
63-
[!code-csharp[FxCop.Usage.CallGCSuppressFinalizeCorrectly2#1](../code-quality/codesnippet/CSharp/ca1816-call-gc-suppressfinalize-correctly_2.cs)]
76+
## Related rules
6477

65-
## Related Rules
66-
[CA2215: Dispose methods should call base class dispose](../code-quality/ca2215-dispose-methods-should-call-base-class-dispose.md)
78+
- [CA2215: Dispose methods should call base class dispose](../code-quality/ca2215-dispose-methods-should-call-base-class-dispose.md)
79+
- [CA2216: Disposable types should declare finalizer](../code-quality/ca2216-disposable-types-should-declare-finalizer.md)
6780

68-
[CA2216: Disposable types should declare finalizer](../code-quality/ca2216-disposable-types-should-declare-finalizer.md)
81+
## See also
6982

70-
## See Also
71-
[Dispose Pattern](/dotnet/standard/design-guidelines/dispose-pattern)
83+
- [Dispose pattern](/dotnet/standard/design-guidelines/dispose-pattern)

0 commit comments

Comments
 (0)