Error : Cannot write to a closed TextWriter
-
Hi, I am getting the above mentioned error while excuting the following simple example ( relating to dispose and finalize). Exact output is also attached after the program. Can any body explain me the cause for this? Thanks a lot in advance //#PROGRAM using System; namespace test { //Base.cs public class Base : IDisposable { private bool disposed = false; private readonly int id; public int ID { get { return id; } } public Base(int theID) { id = theID; } public void Disp() { Console.WriteLine("You can run me after disposing{0}",id); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { Console.WriteLine("Base Cleaning up managed resources on {0}",id); // Code to clean up managed resources } Console.WriteLine("Base Cleaning up unmanaged resources on {0}", id); // Code to clean up unmanaged resources } disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~Base() { //Following line gives error for object 2 Console.WriteLine("*** Finalize called on Base {0}", id); Dispose(false); } } //Derived.cs public class Derived : test.Base { private bool disposed = false; public Derived(int theID) : base(theID) {} protected override void Dispose(bool disposing) { if (!disposed) { try { if (disposing) { Console.WriteLine("Derived Cleaning up managed resources"); // Code to clean up managed resources } Console.WriteLine("Derived Cleaning up unmanaged resources"); // Code to clean up unmanaged resources } finally { base.Dispose(disposing); } } disposed = true; } } //Test.cs class Test { [STAThread] static void Main(string[] args) { Derived object1 = new Derived(1); Derived object2 = new Derived(2); object1.Dispose(); } } } //#OUTPUT Derived Cleaning up managed resources Derived Cleaning up unmanaged resources Base Cleaning up managed resources on 1 Base Cleaning up unmanaged resources on 1 An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll Additional information: Cannot write to a closed TextWriter. Unhandled Exception: System.ObjectDisposedException: Cannot write to a closed TextWriter. at System.IO.__Error.WriterClosed() at System.IO.
-
Hi, I am getting the above mentioned error while excuting the following simple example ( relating to dispose and finalize). Exact output is also attached after the program. Can any body explain me the cause for this? Thanks a lot in advance //#PROGRAM using System; namespace test { //Base.cs public class Base : IDisposable { private bool disposed = false; private readonly int id; public int ID { get { return id; } } public Base(int theID) { id = theID; } public void Disp() { Console.WriteLine("You can run me after disposing{0}",id); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { Console.WriteLine("Base Cleaning up managed resources on {0}",id); // Code to clean up managed resources } Console.WriteLine("Base Cleaning up unmanaged resources on {0}", id); // Code to clean up unmanaged resources } disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~Base() { //Following line gives error for object 2 Console.WriteLine("*** Finalize called on Base {0}", id); Dispose(false); } } //Derived.cs public class Derived : test.Base { private bool disposed = false; public Derived(int theID) : base(theID) {} protected override void Dispose(bool disposing) { if (!disposed) { try { if (disposing) { Console.WriteLine("Derived Cleaning up managed resources"); // Code to clean up managed resources } Console.WriteLine("Derived Cleaning up unmanaged resources"); // Code to clean up unmanaged resources } finally { base.Dispose(disposing); } } disposed = true; } } //Test.cs class Test { [STAThread] static void Main(string[] args) { Derived object1 = new Derived(1); Derived object2 = new Derived(2); object1.Dispose(); } } } //#OUTPUT Derived Cleaning up managed resources Derived Cleaning up unmanaged resources Base Cleaning up managed resources on 1 Base Cleaning up unmanaged resources on 1 An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll Additional information: Cannot write to a closed TextWriter. Unhandled Exception: System.ObjectDisposedException: Cannot write to a closed TextWriter. at System.IO.__Error.WriterClosed() at System.IO.
Is the console being routed somewhere else? Writing to the console is causing an issue, an ObjectDisposedException, as if the console itself was disposed. Is the application exiting or something when you're writing to the console? If this is a console application, try adding a Console.ReadLine() at the end of your Main method. That way it won't exit until you press a key.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Hi, I am getting the above mentioned error while excuting the following simple example ( relating to dispose and finalize). Exact output is also attached after the program. Can any body explain me the cause for this? Thanks a lot in advance //#PROGRAM using System; namespace test { //Base.cs public class Base : IDisposable { private bool disposed = false; private readonly int id; public int ID { get { return id; } } public Base(int theID) { id = theID; } public void Disp() { Console.WriteLine("You can run me after disposing{0}",id); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { Console.WriteLine("Base Cleaning up managed resources on {0}",id); // Code to clean up managed resources } Console.WriteLine("Base Cleaning up unmanaged resources on {0}", id); // Code to clean up unmanaged resources } disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~Base() { //Following line gives error for object 2 Console.WriteLine("*** Finalize called on Base {0}", id); Dispose(false); } } //Derived.cs public class Derived : test.Base { private bool disposed = false; public Derived(int theID) : base(theID) {} protected override void Dispose(bool disposing) { if (!disposed) { try { if (disposing) { Console.WriteLine("Derived Cleaning up managed resources"); // Code to clean up managed resources } Console.WriteLine("Derived Cleaning up unmanaged resources"); // Code to clean up unmanaged resources } finally { base.Dispose(disposing); } } disposed = true; } } //Test.cs class Test { [STAThread] static void Main(string[] args) { Derived object1 = new Derived(1); Derived object2 = new Derived(2); object1.Dispose(); } } } //#OUTPUT Derived Cleaning up managed resources Derived Cleaning up unmanaged resources Base Cleaning up managed resources on 1 Base Cleaning up unmanaged resources on 1 An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll Additional information: Cannot write to a closed TextWriter. Unhandled Exception: System.ObjectDisposedException: Cannot write to a closed TextWriter. at System.IO.__Error.WriterClosed() at System.IO.
p.s. Next time surround your code with <pre> tags.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
p.s. Next time surround your code with <pre> tags.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango