On C# Dispose Pattern


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
 
Ad

Advertisements

E

eliza sahoo

Dispose pattern is nothing new but a better approach of disposing managed as well as unmanaged objects. This has been referred by the MSDN and meant for a better architecture towards object disposal in .Net applications.

The backbone of .Net applications, the CLR, does not have any control over the unmanaged resources. It can?t dispose them automatically as it does for managed ones. Hence it needs to be done explicitly by the programmer.

The proposed architecture was given by the MSDN for a flawless design and to avoid memory leaks. It provides the derived classes a chance to dispose their unmanaged (also managed) resources, if they have.


Example:
Snippet 1:

public class BaseClass : IDisposable
{
bool isDisposed = false;
public BaseClass()
{
//
// TODO: Add constructor logic here
//
}

#region IDisposable Members

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

public virtual void Dispose(bool doingDispose)
{
if (!this.isDisposed)
{
if (doingDispose)
{
//Release all managed resources
}
//Release all Unmanaged resources over here.
//So if doingDispose is FALSE, then only unmanaged resources will be released.
}
this.isDisposed = true;
}

//Destructor
~BaseClass()
{
this.Dispose(false);
}

#endregion
}

Snippet 2:

public class DerivedClass : BaseClass
{
private bool isDisposed = false;
public DerivedClass()
{
//
// TODO: Add constructor logic here
//
}

public override void Dispose(bool doingDispose)
{
if (!this.isDisposed)
{
if (doingDispose)
{
// Release the managed resources you added in
// this derived class here.
}
// Release the native unmanaged resources you added
// in this derived class here
}
}
}

Explanation:

1. The above example is the recommended design pattern for Disposing objects and has been implemented through out the .Net Framework.
2. Here the BaseClass has a virtual Dispose method which gives the DerivedClass a chance to dispose the unmanaged resources.
3. Again the virtual Dispose class gets executed in two scenarios with true and false parameters.
4. In normal scenario the Dispose methods gets executed with true. It happens when an instance calls the Dispose() method, which in turn calls the Dispose(true) method.
5. For false, the Dispose(false) is called only at runtime from the finalize block(destructor) and disposes only unmanaged resources.




NvrBst wrote:

On C# Dispose Pattern
23-Feb-08

I would like some clarification on the dispose pattern just so I ca
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

Previous Posts In This Thread:

On C# Dispose Pattern
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


Submitted via EggHeadCafe - Software Developer Portal of Choice
Sending SMTP email from within BizTalk Orchestration
http://www.eggheadcafe.com/tutorial...f-1716445b26bc/sending-smtp-email-from-w.aspx
 
Ad

Advertisements

E

eliza sahoo

Dispose pattern is nothing new but a better approach of disposing managed as well as unmanaged objects. This has been referred by the MSDN and meant for a better architecture towards object disposal in .Net applications.

The backbone of .Net applications, the CLR, does not have any control over the unmanaged resources. It can?t dispose them automatically as it does for managed ones. Hence it needs to be done explicitly by the programmer.

The proposed architecture was given by the MSDN for a flawless design and to avoid memory leaks. It provides the derived classes a chance to dispose their unmanaged (also managed) resources, if they have.


Example:
Snippet 1:

public class BaseClass : IDisposable
{
bool isDisposed = false;
public BaseClass()
{
//
// TODO: Add constructor logic here
//
}

#region IDisposable Members

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

public virtual void Dispose(bool doingDispose)
{
if (!this.isDisposed)
{
if (doingDispose)
{
//Release all managed resources
}
//Release all Unmanaged resources over here.
//So if doingDispose is FALSE, then only unmanaged resources will be released.
}
this.isDisposed = true;
}

//Destructor
~BaseClass()
{
this.Dispose(false);
}

#endregion
}

Snippet 2:

public class DerivedClass : BaseClass
{
private bool isDisposed = false;
public DerivedClass()
{
//
// TODO: Add constructor logic here
//
}

public override void Dispose(bool doingDispose)
{
if (!this.isDisposed)
{
if (doingDispose)
{
// Release the managed resources you added in
// this derived class here.
}
// Release the native unmanaged resources you added
// in this derived class here
}
}
}

Explanation:

1. The above example is the recommended design pattern for Disposing objects and has been implemented through out the .Net Framework.
2. Here the BaseClass has a virtual Dispose method which gives the DerivedClass a chance to dispose the unmanaged resources.
3. Again the virtual Dispose class gets executed in two scenarios with true and false parameters.
4. In normal scenario the Dispose methods gets executed with true. It happens when an instance calls the Dispose() method, which in turn calls the Dispose(true) method.
5. For false, the Dispose(false) is called only at runtime from the finalize block(destructor) and disposes only unmanaged resources.




NvrBst wrote:

On C# Dispose Pattern
23-Feb-08

I would like some clarification on the dispose pattern just so I ca
be sure I understand it correctly. Basically the following

1---If the Class is sealed (or don't plan on it ever being a bas
class then
private bool disposed = false
public void Dispose()
if(!disposed) {/*CLEAN*/; disposed=true;


2---If the Class might have derived classe
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 clas
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. I
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 hav
unmanaged objects? And, if there is UnManaged Clean Up needed then
will always need a Finallizer

4---UnManaged If the Class doesn't extend from a IDisposabl
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

Previous Posts In This Thread:

On C# Dispose Pattern
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

Dispose pattern for .Net Applications
Dispose pattern is nothing new but a better approach of disposing managed as well as unmanaged objects. This has been referred by the MSDN and meant for a better architecture towards object disposal in .Net applications.

The backbone of .Net applications, the CLR, does not have any control over the unmanaged resources. It can?t dispose them automatically as it does for managed ones. Hence it needs to be done explicitly by the programmer.

The proposed architecture was given by the MSDN for a flawless design and to avoid memory leaks. It provides the derived classes a chance to dispose their unmanaged (also managed) resources, if they have.


Example:
Snippet 1:

public class BaseClass : IDisposable
{
bool isDisposed = false;
public BaseClass()
{
//
// TODO: Add constructor logic here
//
}

#region IDisposable Members

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

public virtual void Dispose(bool doingDispose)
{
if (!this.isDisposed)
{
if (doingDispose)
{
//Release all managed resources
}
//Release all Unmanaged resources over here.
//So if doingDispose is FALSE, then only unmanaged resources will be released.
}
this.isDisposed = true;
}

//Destructor
~BaseClass()
{
this.Dispose(false);
}

#endregion
}

Snippet 2:

public class DerivedClass : BaseClass
{
private bool isDisposed = false;
public DerivedClass()
{
//
// TODO: Add constructor logic here
//
}

public override void Dispose(bool doingDispose)
{
if (!this.isDisposed)
{
if (doingDispose)
{
// Release the managed resources you added in
// this derived class here.
}
// Release the native unmanaged resources you added
// in this derived class here
}
}
}

Explanation:

1. The above example is the recommended design pattern for Disposing objects and has been implemented through out the .Net Framework.
2. Here the BaseClass has a virtual Dispose method which gives the DerivedClass a chance to dispose the unmanaged resources.
3. Again the virtual Dispose class gets executed in two scenarios with true and false parameters.
4. In normal scenario the Dispose methods gets executed with true. It happens when an instance calls the Dispose() method, which in turn calls the Dispose(true) method.
5. For false, the Dispose(false) is called only at runtime from the finalize block(destructor) and disposes only unmanaged resources.


Submitted via EggHeadCafe - Software Developer Portal of Choice
EggHeadCafe Chat Chaos in Silverlight Released Today
http://www.eggheadcafe.com/tutorial...6-54f31bdede5d/eggheadcafe-chat-chaos-in.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top