dispose

Do I need to call Close() on a ManualResetEvent?

I've been reading up on .NET Threading and was working on some code that uses a ManualResetEvent. I have found lots of code samples on the internet. However, when reading the documentation for WaitHandle, I saw the following: WaitHandle implements the Dispose pattern. See Implementing Finalize and Dispose to Clean Up Unmanaged ...

Can I "inline" a variable if it's IDisposible?

Do I have to do this to ensure the MemoryStream is disposed of properly? using (MemoryStream stream = new MemoryStream(bytes)) using (XmlReader reader = XmlReader.Create(stream)) { return new XmlDocument().Load(reader); } or is it OK to inline the MemoryStream so that it simply goes out of scope? Like this? using (XmlR...

WPF: Should I manually call Close on windows opened by ShowDialog method?

The thing is that WPF Window doesn't implement IDisposable interface which led me to believe, that I don't have to manually dispose of it when I open it by calling ShowDialog() but the first comment on this MSDN page states differently. Does anybody know what's the truth? ...

enterprise library logging in C++ - logwriter->Dispose() error

I'm using Microsoft's enterprise library logging to log to a rolling flat file listener in my C++ dll. (IDE - VS2005) I'm having an issue that new files keep getting generated, with the GUID appended to the beginning of the file name. I've been googling this issue, and the solution seems to be to call logwriter.Dispose(). For some rea...

Autofac: How to limit the lifetime of an IDisposable object without passing around the IoC container

I'm currently learning how to use Autofac, and I'm stuck with disposing IDisposable objects deterministically. Let me first present the situation before I'll state my problem. Starting position: Let's say my object model is defined through the following interfaces: interface IApple : IDisposable { void Consume(); } interface IHor...

Implement Dispose or Finalize?

Class ComponentsContainer ' a component contains other components' Inherits System.ComponentModel.Component Private foo as New Component Private bar as New Component Protected Override Sub Finalize() foo.Dispose() ' HERE ? ' bar.Dispose() MyBase.Finalize() End Sub Protected Overrides Sub Dispose(disposing As...

Any sense to set obj = null(Nothing) in Dispose()?

Is there any sense to set custom object to null(Nothing in VB.NET) in the Dispose() method? Could this prevent memory leaks or it's useless?! Let's consider two examples: public class Foo : IDisposable { private Bar bar; // standard custom .NET object public Foo(Bar bar) { this.bar = bar; } public void Dispose(...

What is the benefit of disposing of a LINQ to SQL DataContext?

What is the benefit of disposing of a LINQ to SQL DataContext? Or, is there a problem with not disposing of these DataContext objects? For instance, for easy coding, I might want to do something like... var list = from p in (new MyDataContext()).People where p.LastName.Contains("sommar") select p; In this case, I have new'd up an ins...

How to dispose managed resource in Dispose() method in C#?

Hi, I know Dispose() is intended for unmanaged resource, and the resource should be disposed when it is no longer needed without waiting for the garbage collector to finalize the object. However, when disposing the object, it suppress finalization of the garbage collector (GC.SuppressFinalize(this); in the code below). This means t...

Are all disposable objects instantiated within a using block disposed?

This is a question I have asked myself many times in the past as I nested using statements 5 deep. Reading the docs and finding no mention either way regarding other disposables instantiated within the block I decided it was a good Q for SO archives. Consider this: using (var conn = new SqlConnection()) { var conn2 = new SqlConne...

VS2005 Code Analysis: CA1063 (call dispose(true) and supress finalize)

I am getting this fxcop warning even though my objects do not have finalizers I just have the simple IDisposable.Dispose() method in place. Is this rule really telling me I should implement finalizers? I don't have any unmanaged resources to finalize so I wouldn't have the need for the advanced Dispose(bool) pattern with suppress finali...

How to solve this potentially dispose related file overwrite issue?

I am loading a 50x50 Bitmap file and then filling it a single random color at program startup. Then save the result onto the same file and assign it to a PictureBox, but running into file write problems and "A generic error occurred in GDI+", etc. How to do this properly so that I can continually repeat this, (open bitmap, paint it rand...

Release com object on runtime error

I'm fiddling round with Excel interop at the moment. Its all going smoothly and I'm disposing of the COM objects and everybody is happy. What I'm wondering though is how I can reliably kill the COM object if my application crashes out? Any suggestions welcome, I'm using C# ...

Java this.dispose not closing window when called.

Hello. I am writing a program from class, and I am attempting to have it set up so a window is created which shows search results in the form of buttons. I would like it if there are no search results, that the window would call a pop-up warning stating such and then just close the window. I have it setup that whenever I want to mak...

Disposing objects in the Destructor

I have an object that has a disposable object as a member. public class MyClass { private MyDisposableMember member; public DoSomething { using (member = new MyDisposableMember()) { // Blah... } } } There can be many methods in MyClass, all requiring a using statement. But what...

Disposing JFrame by clicking from an inner JPanel.

Hello, I'm trying to dispose my JFrame by clicking a button, located on a JPanel that is placed on the JFrame that I want to close. I tried to make a static method on the JFrame class, but ofcourse my IDE told me that wasn't going to happen. Anyone thinking of a solution? Thanks! ...

Correctly disposing user controls and child controls in C#

Hi we are experiencing a few problems with the IDisposable pattern. In this case there is a user control 'ControlA' with a FlowLayoutPanel, which contains more usercontrols 'ControlB'. When calling Dispose(bool), I check if disposing if true, and if IsDisposed is false. I then try to explicitly dispose each ControlB in the FlowLayoutPa...

DI with disposable objects

Suppose my repository class looks like this: class myRepository : IDisposable{ private DataContext _context; public myRepository(DataContext context){ _context = context; } public void Dispose(){ // to do: implement dispose of DataContext } } now, I am using Unity to control the lifetime of my repo...

Unreferenced Thread object dispose

If i use thread like this: void foo() { new Thread().Start(); } since the Thread object is not referenced, will it be disposed by GC before the designated work is done? ...

Dispose of SWT shell when cursor moves out of it

Hi! I'm implementing a custom preview/tooltip for an Eclipse plug-in. It did it using a Shell in SWT, removing all its trimmings and placing a text box inside it. It looks great. However now I need to dispose of the shell when the cursor moves out of the shell window and I ran into some issues: Does it make sense to attach a mousemov...