Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 3.0
Tools
Development Tools
FxCop
FxCop Warnings
Usage Warnings
 Dispose methods should call base cl...
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 methods should call base class dispose

TypeName

DisposeMethodsShouldCallBaseClassDispose

CheckId

CA2215

Category

Microsoft.Usage

Breaking Change

NonBreaking

A type that implements System.IDisposable inherits from a type that also implements IDisposable. The Dispose method of the inheriting type does not call the Dispose method of the parent type.

If a type inherits from a disposable type, it must call the Dispose method of the base type from within its own Dispose method.

To fix a violation of this rule, call base.Dispose in your Dispose method.

It is safe to exclude a warning from this rule if the call to base.Dispose occurs at a deeper calling level than the rule checks.

The following example shows a type TypeA that implements IDisposable.

C#
using System;  

namespace UsageLibrary
{
    public class  TypeA :IDisposable
    {
     
        protected virtual void Dispose(bool disposing) 
        {
            if (disposing) 
            {
                // Dispose managed resources
            }

            // Free native resources
        }

        public void Dispose()
        {

                Dispose(true);

                GC.SuppressFinalize(this);

        }

        // Disposable types implement a finalizer.
        ~TypeA()
        {
            Dispose(false);
        }
    }
}

The following example shows a type TypeB that inherits from type TypeA and correctly calls its Dispose method.

Visual Basic
Imports System

Namespace UsageLibrary

    Public Class TypeB
        Inherits TypeA

        Friend Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                Dispose(False)
            Finally
                MyBase.Finalize()
            End Try
        End Sub 'Dispose
    End Class 'TypeB

End Namespace
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Correct VB Implementation      Peter Ritchie   |   Edit   |   Show History

As provided, the VB code for TypeB will not compile based on the C# code for TypeA; Friend Dispose is a different access level from TypeA.Dispose (which is Protected, not Friend). Plus, TypeB.Dispose actually causes infinite recursion and eventually StackOverflowException. Following is correct code:

 Friend Class TypeB
Inherits TypeA
  Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing Then
' dispose of managed resources
End If
' free native resources.
Finally
MyBase.Dispose(disposing)
End Try
End Sub 'Dispose
End Class 'TypeB
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker