N
NvrBst
I would like some clarification on the dispose pattern just so I can
be sure I understand it correctly. Basically the following:
1---If the Class is sealed (or don't plan on it ever being a base
class then)
private bool disposed = false;
public void Dispose() {
if(!disposed) {/*CLEAN*/; disposed=true;}
}
2---If the Class might have derived classes
private bool dispoed = false;
public void Disposed() { Dispose(true); }
protected virtual void Dispose(bool disposing) {
if(!dispsoed) {if(disposing) {/*CLEAN*/;} disposed=true;}
}
3---If the Class is extened from a IDisposable class (Only If class
allocates new IDisposable Objects)
private bool dispoed = false;
protected override void Dispose(bool disposing) {
if(!disposed) {if(disposing) {/*CLEAN YOUR STUFF*/;}
disposed=true;}
base.Dispose(disposing);
}
Assuming no unmanaged cleanup, I think the top is all correct. If
anyone notices a mistake or something you think I might of missed,
please interrupt anytime.
Question 1: Would there ever be a finalizer if my class doesn't have
unmanaged objects? And, if there is UnManaged Clean Up needed then I
will always need a Finallizer?
4---UnManaged If the Class doesn't extend from a IDisposable
private bool disposed = false;
public void Disposed() { Dispose(true); GC.SupressFinalizer(true); }
protected virtual void Dispose(bool disposing) {
if(!dispsoed) {if(disposing) {/*CLEAN Managed*/;} /*CLEAN
UnManaged*/ disposed=true;}
}
~Class() { Dispose(false); }
5---UnManaged If the Class is extened from a IDisposable class (Only
If class allocates new IDisposable Objects)
private bool dispoed = false;
protected override void Dispose(bool disposing) {
if(!disposed) {if(disposing) {/*CLEAN You Managed*/;} /*CLEAN Your
UnManaged*/ disposed=true;}
base.Dispose(disposing);
}
~Class() { Dispose(false); }
Question 2: If the Class is sealed, is there a simpler version than
"4" if UnManaged clean up is need?
Question 3: The GC.SupressFinalizer(true); would only ever be in the
"Disposed()" method if I have a "~Class()"?
Question 4: I'm a little confused on what is Managed CleanUp, and
whats UnManaged. Anything with a ".Dispose()" method would be managed
clean up. Any WinAPI pInvoke (that needs a ReleaseHandle or Close)
would be UnManaged; assuming I don't use a SafeHandle with it. Any
extra clean up (IE Flushing a Managed Stream, Stoping Possible Managed
Threads, UnRegistring Global System HotKeys, Maybe setting large
objects (that don't have dispose) to null) would all go in the
UnManaged CleanUp section?
Basically anything without a .Dispose() that I want to do would go in
UnManaged CleanUp? If this is the case, then would I always need a
finalizer if I have items in the /*CLEAN UnManaged*/ section? How do
I choose I need a Finalizer?
I think I understand most the other rules already (Like Dispose()
should be able to be run multiple times without error'ing, etc).
Thanks for any help on clarifing the details for me
NB
be sure I understand it correctly. Basically the following:
1---If the Class is sealed (or don't plan on it ever being a base
class then)
private bool disposed = false;
public void Dispose() {
if(!disposed) {/*CLEAN*/; disposed=true;}
}
2---If the Class might have derived classes
private bool dispoed = false;
public void Disposed() { Dispose(true); }
protected virtual void Dispose(bool disposing) {
if(!dispsoed) {if(disposing) {/*CLEAN*/;} disposed=true;}
}
3---If the Class is extened from a IDisposable class (Only If class
allocates new IDisposable Objects)
private bool dispoed = false;
protected override void Dispose(bool disposing) {
if(!disposed) {if(disposing) {/*CLEAN YOUR STUFF*/;}
disposed=true;}
base.Dispose(disposing);
}
Assuming no unmanaged cleanup, I think the top is all correct. If
anyone notices a mistake or something you think I might of missed,
please interrupt anytime.
Question 1: Would there ever be a finalizer if my class doesn't have
unmanaged objects? And, if there is UnManaged Clean Up needed then I
will always need a Finallizer?
4---UnManaged If the Class doesn't extend from a IDisposable
private bool disposed = false;
public void Disposed() { Dispose(true); GC.SupressFinalizer(true); }
protected virtual void Dispose(bool disposing) {
if(!dispsoed) {if(disposing) {/*CLEAN Managed*/;} /*CLEAN
UnManaged*/ disposed=true;}
}
~Class() { Dispose(false); }
5---UnManaged If the Class is extened from a IDisposable class (Only
If class allocates new IDisposable Objects)
private bool dispoed = false;
protected override void Dispose(bool disposing) {
if(!disposed) {if(disposing) {/*CLEAN You Managed*/;} /*CLEAN Your
UnManaged*/ disposed=true;}
base.Dispose(disposing);
}
~Class() { Dispose(false); }
Question 2: If the Class is sealed, is there a simpler version than
"4" if UnManaged clean up is need?
Question 3: The GC.SupressFinalizer(true); would only ever be in the
"Disposed()" method if I have a "~Class()"?
Question 4: I'm a little confused on what is Managed CleanUp, and
whats UnManaged. Anything with a ".Dispose()" method would be managed
clean up. Any WinAPI pInvoke (that needs a ReleaseHandle or Close)
would be UnManaged; assuming I don't use a SafeHandle with it. Any
extra clean up (IE Flushing a Managed Stream, Stoping Possible Managed
Threads, UnRegistring Global System HotKeys, Maybe setting large
objects (that don't have dispose) to null) would all go in the
UnManaged CleanUp section?
Basically anything without a .Dispose() that I want to do would go in
UnManaged CleanUp? If this is the case, then would I always need a
finalizer if I have items in the /*CLEAN UnManaged*/ section? How do
I choose I need a Finalizer?
I think I understand most the other rules already (Like Dispose()
should be able to be run multiple times without error'ing, etc).
Thanks for any help on clarifing the details for me
NB