Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
 Dispose objects before losing scope
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Team System
Dispose objects before losing scope

TypeName

DisposeObjectsBeforeLosingScope

CheckId

CA2000

Category

Microsoft.Reliability

Breaking Change

NonBreaking

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

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.

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

Note that you can use the using statement (Using in Visual Basic) to wrap objects that implement IDisposable. Objects wrapped in this manner will automatically be disposed at the close of the using block.

Do not exclude a warning from this rule, unless you have called a method on your object that calls Dispose, such as Stream.Close

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
False Positive      bdukes   |   Edit   |   Show History
This rule doesn't recognize all situations where a disposable resource is not owned by the thread. Examples being a StringReader embedded within an XmlReader, or assigning a new Font object to a WinForms control's Font property.
False Positive      Jonathan Allen   |   Edit   |   Show History
This can occur when one is creating and showing a form in a method.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker