dispose

Dispose of a .NET application before Application.Run is complete

I have a project that shows a UI when no proper command line arguments are passed. It's is a simple utility built to dynamically update a desktop wallpaper. When it's done with its update I call this.Dispose(); but this causes an issue if command line arguments are passed. The form actually ends up getting the Dispose method call bef...

What are the kind of variables that must be disposed? (.NET/Java)

Three questions: What kind of variables should be disposed manually in .NET/Java? I know that SqlConnection should always be either disposed manually or used in a using{} block. Is it right? What are the other kind of variables that should be disposed? I read somewhere that unmanaged code must be disposed manually. Is that right? What ...

Do you need to call Dispose() on IWindsorContainer?

I just noticed that the IWindsorContainer interface in Castle Windsor includes Dispose(). None of the CastleWindsor tutorials I have seen (e.g. http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx and http://wiki.bittercoder.com/ContainerTutorials.ashx) declare th...

IWindsorContainer - when to call Release().

When I have resolved a component using container.Resolve(), and I have finished using it, should I call Release()? At the moment I call Dispose on any IDisposable that the container has got for me. Should I not call Dispose() on the object, but instead call Release() on the container instead? Does it do the same thing? Thanks David ...

Do I need to implement a dispose or finalize in my objects?

For too long I let the garbage collector do its magic, removing all responsibilities from my self. Sadly it never turned into an issue... So I never gave a second thought to the subject. Now when I think about it I don't really understand what the "dispose" function really does and how and when it should be implemented. The same quest...

Do I need to Dispose XmlReader if I Dispose its underlying Stream?

I have the following method GetData that creates a StreamReader from a file. private void GetData(string file) { string filename = Path.GetFileNameWithoutExtension(file); XmlDocument xmldoc = new XmlDocument(); using (StreamReader sr = new StreamReader(file)) { Stream bs = sr.BaseStream; Stream cl = main...

.Net - Dispose correctly children object

From code analysis (Visual studio), I got this warning: Warning 2 CA2000 : Microsoft.Reliability : ... Call System.IDisposable.Dispose on object 'l' before all references to it are out of scope... So, I changed the code : Dim l As LiteralControl = New LiteralControl AddHandler l.DataBinding, AddressOf OnDataBinding container.Control...

Can Compilers targeting CLR generate Dispose method call when object is set to null?

Can compilers (for e.g. C#) automatically generate call to Dispose method on the object when it is set to null (of course, object should support Dispose method in first place). For example, if we write cnSqlConnection = null; and cnSqlConnection is an instance of type SqlConnection, can C# compiler inject Dispose method call right befo...

Is it better to use the [using] statement in C# or the [dispose] method? Does this apply to external (COM) objects?

What is better, the using directive, or the dispose directive when finished with an object? using(FileStream fileStream = new FileStream( "logs/myapp.log", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using(StreamReader streamReader = new StreamReader(fileSt...

C# Socket.Close - Should I still call Dispose?

Should I still call Dispose() on my socket after closing it? For example: mySocket.Shutdown(SocketShutdown.Both); mySocket.Close(); mySocket.Dispose(); // Redundant? I was wondering because the MSDN documentation says the following: Closes the Socket connection and releases all associated resources. Thanks. ...

why dispose method is not for string object?

why dispose method is not for string object in c#? As we know Dispose() is the method for dispose the object. But why it is not allows for string object or integer object? Edited: What does the mean of managed object ?. Please guide me. ...

Dispose form after closing.

Dear friends I have got the new problem with opening and closing form in C#. My problem is how to dispose the form after closing . here is my code : Program.cs: static class Program { public static Timer timer; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompati...

Why should Dispose() be non-virtual?

I'm new to C#, so apologies if this is an obvious question. In the MSDN Dispose example, the Dispose method they define is non-virtual. Why is that? It seems odd to me - I'd expect that a child class of an IDisposable that had its own non-managed resources would just override Dispose and call base.Dispose() at the bottom of their own ...

What is the difference between finalize and dispose in .net ?

Possible Duplicate: Finalize vs Dispose Hi, Recently I was quizzed in an interview about finalize and dispose. When is each one of them used and how is Garbage Collector related to them. Kindly share links to enlighten more on the topic. Kindly share ... Thanks in advance. ...

How to properly dispose of SP objects although not assigned?

If I assign SPContext.Current.Site.OpenWeb().Title to a string, do I need to dispose of it (if possible)? string title = SPContext.Current.Site.OpenWeb().Title; I'm still a little fuzzy on when to dispose of sp objects, so I always dispose of my SPWeb and SPSite objects... But, if I don't assign the statement above to an object first,...

How do you deal with sequences of IDisposable using LINQ?

What's the best approach to call Dispose() on the elements of a sequence? Suppose there's something like: IEnumerable<string> locations = ... var streams = locations.Select ( a => new FileStream ( a , FileMode.Open ) ); var notEmptyStreams = streams.Where ( a => a.Length > 0 ); //from this point on only `notEmptyStreams` will be used/v...

What happens to the event of method scoped variable?

Consider the following code: public class Bar { Foo foo; void Go() { foo = new Foo(); foo.Send(...); foo.Dispose(); foo = null; } } public class Foo : IDisposable { public void Send(byte[] bytes) { SocketAsyncEventArgs args = new SocketAsyncEventArgs(); args.SetBuffer(byt...

Disposing and resource management in C#

Hello, I use BackGroundWorker and ProgressBar. private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; e.Result = MyMethod((int)e.Argument, worker, e); } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { progr...

Dispose required if using interface

Let's say I've got a MyObject object that has two interfaces: IMyContract and IDisposable. And I have this code in a method: IMyContract blah = new MyObject(); blah.Blah(); return; This is a potential memory leak, right? Doesn't it need to be: using (MyObject blah = new MyObject()) { blah.Blah(); } return; ...

How to dispose of a SqlDataReader which is bound to a control

I have an SqlDataReader which I use with a data-bound control. Since I'd like to free the DB-connection as soon as it is no longer used, I'm putting the relevant code into a using-block, e.g. as suggested here: using (SqlDataReader reader = getReader()) { databoundControl.DataSource = reader; databoundControl.DataBind(); } ...