1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
sealed
class
MyType : IDisposable
{
// other code
private
bool
_isDisposed;
public
void
SomeMethod()
{
if
(_isDisposed)
throw
new
ObjectDisposedException();
// proceed..
}
public
void
Dispose()
{
if
(_isDisposed)
return
;
// cleanup
_isDisposed =
true
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
sealed
class
HasUnmanaged : IDisposable
{
public
void
Dispose()
{
Dispose(
true
);
// prevent object from being promoted to next Gen/finalizer call
GC.SuppressFinalize(
this
);
}
~HasUnmanaged()
{
LogSomeoneForgotToCallDispose();
Dispose(
false
);
}
private
bool
_isDisposed;
private
void
Dispose(
bool
isDisposing)
{
if
(_isDisposed)
return
;
if
(isDisposing)
{
// dispose of managed resources(can access managed members)
}
// release unmanaged resources
_isDisposed =
true
;
}
}
|
If your type cannot be sealed, then it's time to bring out the big guns. Implement the base type as follows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
class
BaseType : IDisposable
{
Intptr _unmanagedRes = ...
// unmanaged resource
SafeHandle _managedRes = ...
// managed resource
public
void
Dispose()
{
Dispose(
true
);
GC.SuppressFinalize(
this
);
}
~BaseType()
{
Dispose(
false
);
}
private
bool
_isDisposed;
virtual
void
Dispose(
bool
isDisposing)
{
if
(_isDisposed)
return
;
if
(isDisposing)
{
// managed resources dispose
_managedRes.Dispose();
}
// unmanaged resource cleanup
Cleanup(_unmanagedRes);
// null out big fields if any
_unmanagedRes =
null
;
_isDisposed =
true
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
class
MessyDerived : BaseType
{
// ... Derived members
private
bool
_isDisposed;
override
void
Dispose(
bool
isDisposing)
{
try
{
if
(!_isDisposed)
{
if
(isDisposing)
{
// derived managed resources
}
// derived unmanaged resources
_isDisposed =
true
;
}
}
finally
{
base
.Dispose(isDisposing);
}
}
}
class
SimpleDerived : BaseType
{
// ... Derived members
}
|
Of course, there will be edge-cases. But for the most part, this should save you a lot of grief
See also - Joe Duffy's post