Destruktor does it need boodisposed
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Destruktor does it need boodisposed

  1. #1
    Join Date
    Sep 2013
    Posts
    19

    Question Destruktor does it need boodisposed

    Hi, this is mario I try to figure out if that boodispose is really needed.
    So far as I can see, it has only a value if I would have called '.dispose' two times for the same Class.
    In that case both of them ClassA and ClassB would not call again their process to free their objects.
    I dont see anymore reason.
    Can anyone help me here?


    Code:
    using System.Data.SqlClient;
    
    public class ClassA : IDisposable
    {
    	private SqlConnection objCnnA;
    	private string mServerName;
    	private bool mbooDisposed = false;
    
    	public ClassA()
    	{
    		objCnnA = new SqlConnection();
    	}
    
    
    	protected virtual void dispose()
    	{
    		try {
    			this.dispose(true);
    			GC.SuppressFinalize(this);
    		} catch (Exception ex) {
    			MessageBox.Show("ClassA dispose =" + ex.Message);
    		}
    
    	}
    	void IDisposable.Dispose()
    	{
    		dispose();
    	}
    
    
    	protected virtual void Dispose(bool BoolDisposed)
    	{
    		try {
    			if (!(this.mbooDisposed)) {
    				if (BoolDisposed) {
    					if (objCnnA is IDisposable) {
    						(objCnnA as IDisposable).Dispose();
    					}
    
    					if ((objCnnA != null))
    						objCnnA = null;
    					//MyBase.dispose()
    					base.Finalize();
    				}
    			}
    
    			this.mbooDisposed = true;
    
    		} catch (Exception ex) {
    			MessageBox.Show("Basic Finalize =" + ex.Message);
    		}
    	}
    
    	public virtual string ServerName {
    		//overridable: vielleicht wird mehr als eine ClassB deklariert
    
    		get { return mServerName; }
    		set { mServerName = value; }
    	}
    
    	public virtual void OpenCnn()
    	{
    		objCnnA.ConnectionString = "integrated security=SSPI;data source=" + mServerName + ";" + "persist security info=true;initial catalog=DB_France";
    
    		objCnnA.Open();
    
    	}
    
    	public virtual void CloseCnn()
    	{
    		if (objCnnA.State == ConnectionState.Open) {
    			objCnnA.Close();
    		}
    	}
    }
    
    
    using System.Data.SqlClient;
    public class ClassB : ClassA
    {
    
    	private SqlConnection objCnnB;
    
    	private bool disposed = false;
    	public ClassB() : base()
    	{
    		objCnnB = new SqlConnection();
    	}
    
    
    	protected override void dispose()
    	{
    		try {
    			this.Dispose(true);
    			GC.SuppressFinalize(this);
    		} catch (Exception ex) {
    			MessageBox.Show("ClassB. dispose = " + ex.Message);
    		}
    
    	}
    
    
    	protected virtual void Dispose(bool boodisposed)
    	{
    		try {
    			if (!(this.disposed)) {
    				if (boodisposed) {
    					if (objCnnB is IDisposable) {
    						(objCnnB as IDisposable).Dispose();
    					}
    
    					if ((objCnnB != null))
    						objCnnB = null;
    
    				}
    			}
    
    			base.dispose(disposed);
    			this.disposed = true;
    		} catch (Exception ex) {
    			MessageBox.Show("ClassB Dispose Overloads =" + ex.Message);
    		}
    	}
    
    	public override string ServerName {
    		get { return base.ServerName; }
    		set { base.ServerName = value; }
    	}
    
    	public override void OpenCnn()
    	{
    		base.OpenCnn();
    	}
    
    	public override void CloseCnn()
    	{
    		base.CloseCnn();
    	}
    }

  2. #2
    Join Date
    Sep 2013
    Posts
    19

    Re: Destruktor does it need boodisposed

    best answer I found may that.
    Whenever gc is finding an object which doesn't has a referenz for longer,
    it looking for those methode 'finalize'. And will call that method, befor it will
    destroy that object totally. (This method is not within that code above)
    In that case those boodisposed handling will work, so no object shall set be free more than one times.
    (note: finalize is not allways needed, but in case of SQLConnection,
    to set free server sources -> finalize is needed).

    this shall be the form of those finalize methode
    (note: it has to be overrides, no mater if it is within baseclase or inherit )
    It has to be finishd with base.finalize. Thats the last confusion, cause inherit will call the baseclass finalize methode, so what about inherit class?

    protected override void Finalize()
    {
    dispose(False)
    base.Finalize()

    }

  3. #3
    Join Date
    Sep 2013
    Posts
    19

    Question Re: Destruktor does it need boodisposed

    http://msdn.microsoft.com/de-de/libr...=vs.90%29.aspx

    This is almost the best example, cause it has a derived class.
    Just the explication about: why the derived class does not need a finalize method, called with base - is not enough.
    And the letters within the derived class are saying: "not have a Finalize method
    ' or a Dispose method with parameters", but it has a dispose method with paramters.

    I have no hardly doubts, that this is working at last, but hell, why there are no better description

    Furthermore the methode "base.Finalize" will called from derived class, of course, but what makes it able to destroy both classes?

    Code:
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    // Design pattern for a base class.
    public class Base : IDisposable
    {
    
    	// Keep track of when the object is disposed.
    
    	protected bool disposed = false;
    	// This method disposes the base object's resources.
    	protected virtual void Dispose(bool disposing)
    	{
    		if (!this.disposed) {
    			if (disposing) {
    				// Insert code to free managed resources.
    			}
    			// Insert code to free unmanaged resources.
    		}
    		this.disposed = true;
    	}
    
    	#region " IDisposable Support "
    	// Do not change or add Overridable to these methods.
    	// Put cleanup code in Dispose(ByVal disposing As Boolean).
    	public void Dispose()
    	{
    		Dispose(true);
    		GC.SuppressFinalize(this);
    	}
    	protected override void Finalize()
    	{
    		Dispose(false);
    		base.Finalize();
    	}
    	#endregion
    }
    
    
    // Design pattern for a derived class.
    public class Derived : Base
    {
    
    	// This method disposes the derived object's resources.
    	protected override void Dispose(bool disposing)
    	{
    		if (!this.disposed) {
    			if (disposing) {
    				// Insert code to free managed resources.
    			}
    			// Insert code to free unmanaged resources.
    		}
    		base.Dispose(disposing);
    	}
    
    	// The derived class does not have a Finalize method
    	// or a Dispose method with parameters because it inherits
    	// them from the base class.
    }
    Last edited by bothways; September 21st, 2013 at 12:57 PM. Reason: futhermore

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,236

    Re: Destruktor does it need boodisposed

    What is boodisposed?

  5. #5
    Join Date
    Sep 2013
    Posts
    19

    Question Re: Destruktor does it need boodisposed

    Quote Originally Posted by Arjay View Post
    What is boodisposed?
    sorry, thats something I changed, while I have written those posting and forgot it within the title, it has to be 'mbooDisposed'

    The question I had at beginnings, its almost done, seem so(why mbooDispose handling is needed?)
    If the answer is, as I wrote, than why does derived class not need a finalize method?
    I could imagine, that, for reasons derivade class stand never alone(its always together with those base class)
    the GC will find those finalize methode inside base class, fine. But than I'm one a way with two thinks I just have to trust.
    1. finalize methode within base class, is carring derivade class as well?
    2. If GC find derivade class, it will look for a finalize methode and will find and take those within base class, as those which is the one ?

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,236

    Re: Destruktor does it need boodisposed

    What is mbooDisposed? Is it a method param?

  7. #7
    Join Date
    Sep 2013
    Posts
    19

    Question Re: Destruktor does it need boodisposed

    Hi Arjay,
    mbooDisposed is a bool variable, in my first code I have put here.

    But I have to say now, this code has one or more wrong lines, if I compare it with the second code.
    First code has not a public void methode. I would like to put that link here, but could not find again.

    The second code works fine and it works as the description of oop is explained.

    But at last, if I watch the third example called "TestConstructorsAndDestructors",
    there is "thisclass" and it has a methode called "base.finalize" even with explicacion "Call base.Finalize if this is a derived class"
    I follow that and have to see, in that case those protectet finalize, is called, whenever those object will destroye.


    In the second example protectet finalize will never called, but from GC. But this we have stopped with "GC.SuppressFinalize"?

    How is that to explain, why the second example does not need a finalize methode, which is called, as it is in the third example?

  8. #8
    Join Date
    Sep 2013
    Posts
    19

    Re: Destruktor does it need boodisposed

    now, there is nothing more to say?

  9. #9
    Join Date
    Sep 2013
    Posts
    19

    Re: Destruktor does it need boodisposed

    I will put it in a new question, may be that helps

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,236

    Re: Destruktor does it need boodisposed

    See http://lostechies.com/chrispatterson...le-done-right/ for how to implement the pattern correctly. Note the variable naming conventions as those are pretty standard in his example.

    Also read http://stackoverflow.com/questions/2...-the-way-it-is for its design background.

  11. #11
    Join Date
    Sep 2013
    Posts
    19

    Re: Destruktor does it need boodisposed

    You offer that as a correct way, but there is no explication to my question.
    A finalize methode has been within every examples I found, and was allways called as something which is necessary.

    The webside you offer, as a correct way, should have any explicion - why finalize is not needed.
    There is no finalize methode, not within those base class, nor derivede class. Eventhough derivede class has letters:"a finalizer is not necessary, as it is inherited from // the base class".

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,236

    Re: Destruktor does it need boodisposed


  13. #13
    Join Date
    Sep 2013
    Posts
    19

    Re: Destruktor does it need boodisposed

    Your answers, are common answers of those, who are unable to analyze any simple thing.
    And you put shame on those who are named MVP as well

  14. #14
    Join Date
    Sep 2013
    Posts
    19

    Question Re: Destruktor does it need boodisposed

    watch'd that side
    http://www.devx.com/dotnet/Article/33167

    tried to set up a comment, but hasn't work. I've send a message to "devx"some days ago, so I put it here.

    I have read all pages but havn't found what I was looking for.

    The first page talks about a database connection example,
    but just about client staff not server. But Server resources need to be care as well.
    Thats one of most reasons to implement those finalize methode. A reason beside, maybe the
    developer has forgotten to call the dispose methode, in any case. In those cases, when GC is finding
    this object, it will look for a finalize methode and so, the whole process, which set free server resources, will start as well.

    But for any reason most webside examples doesn't have a finalize methode, sometimes just
    in the first example.
    And later they don't talk about why its not needed for longer

    What's allways missd: Which one are the code lines, which set free server resources?

    Every webside talks almost about the same. They carring about those dipose variable...
    here there is unmanged and here managed staff
    But for Jesus christ they dont talk about causal relationship.

    base.finalize what is it mean? within derived class it will call the finalize methode
    from the base clase, but "base.finlize" within there..."
    What happend, what is it doing? Is this what shall set free server resources?

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,236

    Re: Destruktor does it need boodisposed

    See http://reedcopsey.com/series/idisposable/

    This is the best article out there on IDisposable, imo. I knew it existed, but wasn't able to find it earlier.

    Should answer where and when a Finalizer is needed and where to clean up resources.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center