“Object can be disposed of more than once” error

I struggled with this problem and found the example here to be very helpful. I’ll post the code for a quick view: using (Stream stream = new FileStream(“file.txt”, FileMode.OpenOrCreate)) { using (StreamWriter writer = new StreamWriter(stream)) { // Use the writer object… } } Replace the outer using statement with a try/finally making sure to … Read more

How to dispose a class in .net?

IDisposable has nothing to do with freeing memory. IDisposable is a pattern for freeing unmanaged resources — and memory is quite definitely a managed resource. The links pointing to GC.Collect() are the correct answer, though use of this function is generally discouraged by the Microsoft .NET documentation. Edit: Having earned a substantial amount of karma … Read more

What’s the purpose of the components IContainer generated by the Winforms designer?

When you add non-UI components to the form (such as a Timer component), components will be the parent of those compoments. The code in the designer file makes sure that these components are disposed of when the form is disposed. If you have not added any such components to the form in design time, components … Read more

What’s the point of overriding Dispose(bool disposing) in .NET?

The full pattern including a finalizer, introduction of a new virtual method and “sealing” of the original dispose method is very general purpose, covering all bases. Unless you have direct handles on unmanaged resources (which should be almost never) you don’t need a finalizer. If you seal your class (and my views on sealing classes … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)