Results 1 to 9 of 9

Thread: Finalizers?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Finalizers?

    Can someone show me how to implement a finalizer in C#. From what I remember the IDisposable interface needs to be implemented. Java has finalizers built in.
    Code:
     import java.io.*; 
    
     public class ConsoleConnector{
      public static void main(String[] args){new ConsoleConnector().connect();}
    
      private BufferedReader buff = null;
      private String input = null; 
     
      public ConsoleConnector(){ 
      buff = new BufferedReader(new InputStreamReader(System.in));
      } 
     public void connect(){
      while(true){
       try{
        input = buff.readLine();
        if(input.equals("Q")) break; 
        System.out.println(input);  
       }catch(IOException ioe){
        System.err.println(ioe); 
       } 
      } 
     }
      protected void finalize(){
       if(buff != null){
        try{
        buff.close(); 
       }catch(IOException ioe){
         System.err.println(ioe);
        }
       }   
      }
     }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Finalizers?

    There is a help topic entitled "Implementing Finalize and Dispose to Clean Up Unmanaged Resources" that you may find useful. There is another entitled "Finalize Methods and Destructors". I found these by doing a help search for "finalize".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196

    Re: Finalizers?

    roughly...

    Code:
    public class ConsoleConnector:IDisposable
    	{
    		//constructor
    		public ConsoleConnector(){
    		}
    		
    		 //finalizer
    		public ~ConsoleConnector(){
    			Dispose(false);
    		}
    
    		public void Connect(){
    			//your connection code
    		}
    		
    		private bool _disposed = false;
    		public void Dispose() {
    			Dispose(true);
    			GC.SuppressFinalize(this);
    		}
    
    		public void Dispose(bool dispose ){
    			if (this._disposed){
    				return;	
    			}
    			if (dispose){
    				//clean up resources here
    			}
    			this._disposed = true;
    		}
    		
    	}


  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Finalizers?

    Seems kinda weird. So the first Disposed is called by the runtime? Also within the second Dispose why would you want to return on true? Then the second if is never executed and nothing is cleaned up.
    Code:
     private bool _disposed = false;
      public void Dispose() {
       Dispose(true);
       GC.SuppressFinalize(this);
      }
    
      public void Dispose(bool dispose ){
       if (this._disposed){
        return;	
       }
       if (dispose){
       //clean up resources here
       }
       this._disposed = true;
      }

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Finalizers?

    The "_disposed" variable is to ensure that disposal only occurs once. I strongly suggest you read the help topics relating to implementing the Dispose and Finalize methods.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196

    Re: Finalizers?

    [the first Dispose is called by the client which calls the overloaded Dispose before instructing the garbage collector NOT to run the finalizer on this object again.

    The second (overloaded) dispose checks the _disposed flag to see if the Dispose method has already been called. If so return - we don't want to clean up resources a second time.

    "Then the second if is never executed and nothing is cleaned up"...its called with a false param from the finalizer/destructor


    Code:
    public ~ConsoleConnector(){
        Dispose(false);
    }


  7. #7

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Finalizers?

    Posted by jmcilhinney

    I strongly suggest you read the help topics relating to implementing the Dispose and Finalize methods.
    Yes yes I am well aware of them.

    It's just a little confusing at first because only one method needs to be implemented when doing this in Java. I would have to put some conditionals within finalize() then suppress if the runtimes makes the first call.

    Thanks for the help guys.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Finalizers?

    The Finalize method is the equivalent to the Java destructor. The Dispose method exists to allow you to explicitly release resources before the object is actually destroyed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Finalizers?

    Posted by jmcilhinney
    The Finalize method is the equivalent to the Java destructor.
    Correct.
    Posted by jmcilhinney
    The Dispose method exists to allow you to explicitly release resources before the object is actually destroyed.
    Same can be in done in Java. But the finalizers() are usually invoked by the runtime.

Posting Permissions

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



Click Here to Expand Forum to Full Width