idisposable

Do objects that implement IDisposable and/or have a Finalizer live until Generation 2 is collected?

I was fairly sure that I had read (in Richter's C# book) that objects that implement IDisposable and/or which have a Finalizer live until Generation 2. However, I can't find the reference, and my test application doesn't seem to support my belief. Can anyone else confirm/deny? ...

ASP MVC: When is IController Dispose() called?

Hey, all! I'm going through a big refactoring / speed tweaking of one of my larger MVC apps. It has been deployed to production for a few months now, and I was starting to get timeouts waiting for connections in the connection pool. I have tracked the issue down to the connections not getting disposed properly. In light of that, I ha...

Making WinForms controls readonly and IDisposable

Hi, I'm wondering whether this is a good thing or a bad thing and what I have to watchout for when calling Dispose. I have a Tab (PageTab) that I extend and insert a Panel which has a listview and another Toolbar control into. These tabs then get inserted into a tab control (all native .NET WinForms controls). When the user closes on...

Is this a legitimate alternative to the "traditional" dispose pattern for class hierarchies?

I am not a fan of boilerplate code: copy-paste reuse is potentially error-prone. Even if you use code snippets or smart templates, there is no guarantee the other developer did, which means there's no guarantee they did it right. And, if you have to see the code, you have to understand it and/or maintain it. What I want to know from the...

When would dispose method not get called?

I was reading this article the other day and was wondering why there was a Finalizer along with the Dispose method. I read here on SO as to why you might want to add Dispose to the Finalizer. My curiousity is, when would the Finalizer be called over the Dispose method itself? Is there a code example or is it based on something happeni...

VB.NET Memory Management

Update: I probably confused memory usage issues with the UI sharing same thread as the processing (as pointed out by MusiGenesis below). However regarding the Memory usage. I am still not able to find VB.net specific syntax, although people have pointed out some great .Net and C# information below (and if I were more versed in those te...

Unity to dispose of object

Is there a way to make Unit dispose property-injected objects as part of the Teardown? The background is that I am working on an application that uses ASP.NET MVC 2, Unity and WCF. We have written our own MVC controller factory that uses unity to instantiate the controller and WCF proxies are injected using the [Dependency] attribute on...

CUDA global memory deallocation issues in .NET

I have a class (see example bellow) which acts as a .NET wrapper for a CUDA memory structure, allocated using cudaMalloc() and referenced using a member field of type IntPtr. (The class uses DllImport of a native C DLL which wraps various CUDA functionality.) The dispose methods checks if the pointer is IntPtr.Zero and if not calls cuda...

Should IDisposable be applied cascadingly?

This is a rather basic question, however I'm still struggling with it a little. IDisposable is implemented, when you want to enable the user of an object to free underlying resources (e.g. sockets etc.) before the object is eventually garbage collected. When i have a class that holds a DbConnection (implements IDisposable), does my cla...

Is mutex correctly implemented and how do I dispose it?

I am reviewing some code and one of the code analysis (fxCop) warnings has gotten me very confused. The code implements a few mutex's by creating variables at the start of the class, similar to this: private Mutex myMutex = new Mutex(); fxCop is popping up with a message saying that I must implement IDisposable for the class as the Mu...

yield return statement inside a using() { } block Disposes before executing

I've written my own custom data layer to persist to a specific file and I've abstracted it with a custom DataContext pattern. This is all based on the .NET 2.0 Framework (given constraints for the target server), so even though some of it might look like LINQ-to-SQL, its not! I've just implemented a similar data pattern. See example be...

How to handle disposable objects we don't have a reference to?

If you have a brush and pen as in: Brush b = new SolidBrush(color); Pen p = new Pen(b); and dispose them like so: b.Dispose(); p.Dispose(); How would you dispose it if it was: Pen p = CreatePenFromColor(color) which would create the brush and pen for you? I can't dispose the brush inside this method, right? Is this a method not t...

Why isn't SqlConnection disposed/closed?

Given the method: internal static DataSet SelectDataSet(String commandText, DataBaseEnum dataBase) { var dataset = new DataSet(); SqlConnection sqlc = dataBase == DataBaseEnum.ZipCodeDb ? new SqlConnection(ConfigurationManager.AppSettings["ZipcodeDB"]) : new SqlConnectio...

Why doesn't the debugger hit this breakpoint consistently? Am I neglecting a file handle?

Consider the following code: static void Main(string[] args) { using (MemoryStream memoryStream = new MemoryStream(Resources.SampleXMLFile)) // Breakpoint set here { using (XmlTextReader xmlTextReader = new XmlTextReader(memoryStream)) { var z = XElement.Load(xmlTextReader); } } Console.ReadLine(); }...

Am I responsible for Disposing a BackgroundImage?

Hi, I have a windows form where I set the BackgroundImage property to a custom bitmap image. private Image MakeCustomBackground() { Bitmap result = new Bitmap(100, 100); using(Graphics canvas = Graphics.FromImage(result)) { // draw the custom image } return result; } private void UpdateFromBackground() { ...

Storing MemoryStream in Cache

I've come across this code in one of my projects, which has a static function to return a MemoryStream from a file, which is then stored in Cache. Now the same class has a constructor which allows to store a MemoryStream in a private variable and later use it. So it looks like this: private MemoryStream memoryStream; public CountryLook...

Testing Finalizers and IDisposable

Hi, The question is how can I test the fact that object disposes resources when finalise is called. The code for the class: public class TestClass : IDisposable { public bool HasBeenDisposed {get; private set; } public void Dispose() { HasBeenDisposed = true; } ~TestClass() { Dispose(); } } Plea...

c# interop marshalling and disposing

I have a DLL, which is designed in C++, included in a C# project and I have strange AccessViolationExceptions happening unrationally. I suspect that my garbage isn't collected correctly. I have an unmanaged method apiGetSettings (from the DLL) which should copy data to a Settings object (actually a struct in the original code, but .NET I...

When doing a Process.Start() do you need to wrap it in a using?

When you are starting a process and dont care about the result is this ok? Process.Start(xxx); Or should you do this using (Process.Start(xxx)){} ...

C#: Inheritance and IDisposable - strange issue

Hi there, can anyone help, i have a small issue, i have an interface and also a base interface, when i try to do .Dispose() It doesn't find the method as its implemented on my sub class NOT base.. and it always seems to want to call the base - even though i specifically put the namespace in front of the parameter on the constructor...