PDA

Click to See Complete Forum and Search --> : Finalizers?


Dillinger4
Aug 8th, 2005, 06:04 PM
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.

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);
}
}
}
}

jmcilhinney
Aug 8th, 2005, 08:19 PM
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".

powdir
Aug 9th, 2005, 08:20 AM
roughly...

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;
}

}

Dillinger4
Aug 9th, 2005, 09:18 AM
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. :confused:

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;
}

jmcilhinney
Aug 9th, 2005, 09:27 AM
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.

powdir
Aug 9th, 2005, 09:31 AM
[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



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

Dillinger4
Aug 10th, 2005, 01:09 AM
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.

jmcilhinney
Aug 10th, 2005, 01:15 AM
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.

Dillinger4
Aug 10th, 2005, 01:31 AM
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.