IDisposable.Dispose メソッド
アンマネージ リソースの解放およびリセットに関連付けられているアプリケーション定義のタスクを実行します。
Sub Dispose()
[C#]
void Dispose();
[C++]
void Dispose();
[JScript]
function Dispose();
解説
このメソッドは、このインターフェイスを実装するクラスのインスタンスが保持する、ファイル、ストリーム、ハンドルなどのアンマネージ リソースを閉じたり解放したりする場合に使用します。通常、このメソッドは、オブジェクトが保持するリソースの解放や、オブジェクトの再利用の準備に関連付けられたすべてのタスクで使用されます。
このメソッドを実装する場合、オブジェクトは、この呼び出しをコンテインメント階層全体に伝達することによって、保持しているリソースがすべて確実に解放されるようにする必要があります。たとえば、オブジェクト A がオブジェクト B を割り当て、オブジェクト B がオブジェクト C を割り当てると、A の Dispose 実装は B の Dispose を呼び出し、さらに C の Dispose も呼び出す必要があります。各オブジェクトはさらに、それぞれの基本クラスが IDisposable を実装する場合は、同クラスの Dispose メソッドを呼び出す必要があります。
オブジェクトの Dispose メソッドが複数回呼び出された場合、そのオブジェクトは最初の呼び出し以外は無視する必要があります。オブジェクトの Dispose メソッドが複数回呼び出された場合、そのオブジェクトは例外をスローできません。リソースが既に解放されているのに Dispose がまだ呼び出されていないことが原因でエラーが発生した場合は、 Dispose は例外をスローできます。
リソース型は、割り当てられている状態と解放されている状態を示すために、特別な規則を使用します。たとえば、ストリーム クラスは従来からオープン状態またはクローズ状態と見なされています。そのような規則を持つクラスは、 Dispose メソッドを呼び出すパブリック メソッドに、Close などのカスタム名を付けて実装することもできます。
Dispose メソッドは明示的に呼び出す必要があるため、 IDisposable を実装するオブジェクトは、 Dispose が呼び出されなかった場合にリソースの解放を処理するファイナライザも実装する必要があります。既定では、メモリを収集する前に、ガベージ コレクタがオブジェクトのファイナライザを自動的に呼び出します。ただし、 Dispose メソッドが呼び出されていれば、破棄されたオブジェクトのファイナライザをガベージ コレクタが呼び出す必要は通常ありません。自動終了されないようにするには、 Dispose 実装で GC.SuppressFinalize メソッドを呼び出します。
ファイナライザ実装の詳細については、 GC クラスと Object.Finalize メソッドの各トピックを参照してください。
使用例
Imports System
Imports System.ComponentModel
' The following example demonstrates how to create
' a resource class that implements the IDisposable interface
' and the IDisposable.Dispose method.
Public Class DisposeExample
' A class that implements IDisposable.
' By implementing IDisposable, you are announcing that
' instances of this type allocate scarce resources.
Public Class MyResource
Implements IDisposable
' Pointer to an external unmanaged resource.
Private handle As IntPtr
' Other managed resource this class uses.
Private component As component
' Track whether Dispose has been called.
Private disposed As Boolean = False
' The class constructor.
Public Sub New(ByVal handle As IntPtr)
Me.handle = handle
End Sub
' Implement IDisposable.
' Do not make this method virtual.
' A derived class should not be able to override this method.
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
' This object will be cleaned up by the Dispose method.
' Therefore, you should call GC.SupressFinalize to
' take this object off the finalization queue
' and prevent finalization code for this object
' from executing a second time.
GC.SuppressFinalize(Me)
End Sub
' Dispose(bool disposing) executes in two distinct scenarios.
' If disposing equals true, the method has been called directly
' or indirectly by a user's code. Managed and unmanaged resources
' can be disposed.
' If disposing equals false, the method has been called by the
' runtime from inside the finalizer and you should not reference
' other objects. Only unmanaged resources can be disposed.
Private Overloads Sub Dispose(ByVal disposing As Boolean)
' Check to see if Dispose has already been called.
If Not Me.disposed Then
' If disposing equals true, dispose all managed
' and unmanaged resources.
If disposing Then
' Dispose managed resources.
component.Dispose()
End If
' Call the appropriate methods to clean up
' unmanaged resources here.
' If disposing is false,
' only the following code is executed.
CloseHandle(handle)
handle = IntPtr.Zero
End If
disposed = True
End Sub
' Use interop to call the method necessary
' to clean up the unmanaged resource.
<System.Runtime.InteropServices.DllImport("Kernel32")> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As [Boolean]
End Function
' This finalizer will run only if the Dispose method
' does not get called.
' It gives your base class the opportunity to finalize.
' Do not provide finalize methods in types derived from this class.
Protected Overrides Sub Finalize()
' Do not re-create Dispose clean-up code here.
' Calling Dispose(false) is optimal in terms of
' readability and maintainability.
Dispose(False)
MyBase.Finalize()
End Sub
End Class
Public Shared Sub Main()
' Insert code here to create
' and use the MyResource object.
End Sub
End Class
[C#]
using System;
using System.ComponentModel;
// The following example demonstrates how to create
// a resource class that implements the IDisposable interface
// and the IDisposable.Dispose method.
public class DisposeExample
{
// A base class that implements IDisposable.
// By implementing IDisposable, you are announcing that
// instances of this type allocate scarce resources.
public class MyResource: IDisposable
{
// Pointer to an external unmanaged resource.
private IntPtr handle;
// Other managed resource this class uses.
private Component component = new Component();
// Track whether Dispose has been called.
private bool disposed = false;
// The class constructor.
public MyResource(IntPtr handle)
{
this.handle = handle;
}
// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
private void Dispose(bool disposing)
{
// Check to see if Dispose has already been called.
if(!this.disposed)
{
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing)
{
// Dispose managed resources.
component.Dispose();
}
// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
CloseHandle(handle);
handle = IntPtr.Zero;
}
disposed = true;
}
// Use interop to call the method necessary
// to clean up the unmanaged resource.
[System.Runtime.InteropServices.DllImport("Kernel32")]
private extern static Boolean CloseHandle(IntPtr handle);
// Use C# destructor syntax for finalization code.
// This destructor will run only if the Dispose method
// does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide destructors in types derived from this class.
~MyResource()
{
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
}
}
public static void Main()
{
// Insert code here to create
// and use the MyResource object.
}
}
[C++]
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::ComponentModel;
// The following example demonstrates how to create
// a resource class that implements the IDisposable interface
// and the IDisposable.Dispose method.
// A base class that implements IDisposable.
// By implementing IDisposable, you are announcing that
// instances of this type allocate scarce resources.
public __gc class MyResource : public IDisposable {
// Pointer to an external unmanaged resource.
IntPtr handle;
// Other managed resource this class uses.
Component* component;
// Track whether Dispose has been called.
bool disposed;
public:
// The class constructor.
MyResource(IntPtr handle) {
this->handle = handle;
disposed = false;
}
// Implement IDisposable*
// Do not make this method virtual.
// A derived class should not be able to this method.
void Dispose() {
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC::SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC::SuppressFinalize(this);
}
private:
// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be disposed.
void Dispose(bool disposing) {
// Check to see if Dispose has already been called.
if (!this->disposed) {
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing) {
// Dispose managed resources.
component->Dispose();
}
// Call the appropriate methods to clean up
// unmanaged resources here.
// If disposing is false,
// only the following code is executed.
CloseHandle(handle);
handle = IntPtr::Zero;
}
disposed = true;
}
// Use interop to call the method necessary
// to clean up the unmanaged resource.
[System::Runtime::InteropServices::DllImport(S"Kernel32")]
static Boolean CloseHandle(IntPtr handle);
// Use C# destructor syntax for finalization code.
// This destructor will run only if the Dispose method
// does not get called.
// It gives your base class the opportunity to finalize.
// Do not provide destructors in types derived from this class.
~MyResource() {
// Do not re-create Dispose clean-up code here.
// Calling Dispose(false) is optimal in terms of
// readability and maintainability.
Dispose(false);
}
};
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard