Questions tagged [dispose]

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources

Microsoft.NET

The pattern for disposing an object, referred to as a dispose pattern, imposes order on the lifetime of an object. The dispose pattern is used only for objects that access unmanaged resources. This is because the garbage collector is very efficient at reclaiming unused managed objects.

A type's Dispose method should release all the resources that it owns. It should also release all resources owned by its base types by calling its parent type's Dispose method. The parent type's Dispose method should release all resources that it owns and in turn call its parent type's Dispose method, propagating this pattern through the hierarchy of base types.

Reference

1987 questions
44
votes
20 answers

How to dispose a class in .net?

The .NET garbage collector will eventually free up memory, but what if you want that memory back immediately? What code do you need to use in a class MyClass to call MyClass.Dispose() and free up all the used space by variables and objects in…
Jorrit Reedijk
  • 598
  • 3
  • 11
  • 22
44
votes
4 answers

How and when are c# Static members disposed?

I have a class with extensive static members, some of which keep references to managed and unmanaged objects. For instance, the static constructor is called as soon as the Type is referenced, which causes my class to spin up a blockingQueue of…
Joe
  • 483
  • 1
  • 4
  • 7
43
votes
1 answer

What is the promise disposer pattern?

I've read about the promise disposer pattern in several places but I can't figure out what it is. It was suggested to me to use it in code that looks like: function getDb(){ return myDbDriver.getConnection(); } var users =…
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
43
votes
10 answers

Is it possible to force the use of "using" for disposable classes?

I need to force the use of "using" to dispose a new instance of a class. public class MyClass : IDisposable { ... } using(MyClass obj = new MyClass()) // Force to use "using" { }
Zanoni
  • 30,028
  • 13
  • 53
  • 73
42
votes
3 answers

C# how to implement Dispose method

I need some advice on the implementation of the Dispose method. In our application the user designs their own UI. I have a preview window that shows what the UI is going to look like. All object drawn in this UI ultimately derive from a common…
WPFNewbie
  • 2,464
  • 6
  • 34
  • 45
42
votes
19 answers

Best way to dispose a list

I am having List object. How can I dispose of the list? For example, List usersCollection =new List(); User user1 = new User(); User user2 = new User() userCollection.Add(user1); userCollection.Add(user2); If I set userCollection =…
Vivekh
  • 4,141
  • 11
  • 57
  • 102
42
votes
7 answers

Dispose, when is it called?

Consider the following code: namespace DisposeTest { using System; class Program { static void Main(string[] args) { Console.WriteLine("Calling Test"); Test(); …
Anemoia
  • 7,928
  • 7
  • 46
  • 71
41
votes
10 answers

What happens if I don't call Dispose on the pen object?

What happens if I don't call Dispose on the pen object in this code snippet? private void panel_Paint(object sender, PaintEventArgs e) { var pen = Pen(Color.White, 1); //Do some drawing }
Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114
41
votes
7 answers

using statement FileStream and / or StreamReader - Visual Studio 2012 Warnings

The new Visual Studio 2012 is complaining about a common code combination I have always used. I know it seems like overkill but I have done the following in my code 'just to be sure'. using (var fs = new FileStream(filePath, FileMode.Open,…
JHubbard80
  • 2,217
  • 1
  • 20
  • 24
40
votes
4 answers

Do I need to Dispose a SemaphoreSlim?

According to the documentation: "a SemaphoreSlim doesn't use a Windows kernel semaphore". Are there any special resources used by the SemaphoreSlim which make it important to call Dispose when the SemaphoreSlim will no longer be used?
Tom Deseyn
  • 1,735
  • 3
  • 17
  • 29
38
votes
9 answers

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…
Sbodd
  • 11,279
  • 6
  • 41
  • 42
38
votes
6 answers

Should I manually dispose the socket after closing it?

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…
Kevin
  • 5,626
  • 3
  • 28
  • 41
37
votes
6 answers

What best practices for cleaning up event handler references?

Often I find myself writing code like this: if (Session != null) { Session.KillAllProcesses(); Session.AllUnitsReady -= Session_AllUnitsReady; Session.AllUnitsResultsPublished -=…
Firoso
  • 6,647
  • 10
  • 45
  • 91
36
votes
2 answers

Does the "using" keyword mean the object is disposed and GC'ed?

I struck up a conversation with my colleague today, who said she'd just learned the reason behind using the using statement. //Using keyword is used to clean up resources that require disposal (IDisposable interface). using (StreamReader reader…
36
votes
5 answers

How bad is it to not dispose() in Powershell?

Sometimes we need to perform small administrative tasks in SharePoint. A simple PowerShell script is a really good tool for that. For instance, such script can enumerate event handlers of a…
naivists
  • 32,681
  • 5
  • 61
  • 85