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
14
votes
2 answers

Do I need to Close and/or Dispose callback channels acquired through OperationContext.Current.GetCallbackChannel?

I'm using OperationContext.Current.GetCallbackChannel to get a channel to the client that called a WCF service operation. Do I need to worry about closing / disposing these callback channels or is this taken care of by the framework?
Peladao
  • 4,036
  • 1
  • 23
  • 43
14
votes
4 answers

Identify IDisposable objects

i have to review a code made by some other person that has some memory leaks. Right now i'm searching the disposable objects to enclause them with the using statement and i would like to know if there is a quick way that tells you all the disposable…
David Espart
  • 11,520
  • 7
  • 36
  • 50
14
votes
2 answers

using on SQLDataReader

I know I asked a related question earlier. I just had another thought. using (SqlConnection conn = new SqlConnection('blah blah')) { using(SqlCommand cmd = new SqlCommand(sqlStatement, conn)) { conn.open(); // *** do I need…
xeshu
  • 788
  • 2
  • 11
  • 24
14
votes
3 answers

Where to call Dispose() of IDisposable created in constructor?

Where to call Dispose() for IDisposable objects owned by an object? public class MyClass { public MyClass() { log = new EventLog { Source = "MyLogSource", Log = "MyLog" }; FileStream stream = File.Open("MyFile.txt",…
Andrej Adamenko
  • 1,650
  • 15
  • 31
14
votes
8 answers

Disposable singleton in C#

I have a singleton that uses the "static readonly T Instance = new T();" pattern. However, I ran into a case where T is disposable, and actually needs to be disposed for unit tests. How can I modify this pattern to support a disposable…
ripper234
  • 222,824
  • 274
  • 634
  • 905
14
votes
2 answers

Why do we need Dispose() method on some object? Why doesn't the garbage collector do this work?

The question is: why do we need to call Dispose() on some objects? Why doesn't the garbage collector collect the object when it goes out of scope? I am trying to understand the reason why it was implemented like that. I mean, wouldn't it be…
Karim
  • 6,113
  • 18
  • 58
  • 83
14
votes
3 answers

Why does this variable need to be set to null after the object is disposed?

The documentation on PowerShell here has the following interesting comment in it: PowerShell powershell = PowerShell.Create(); using (powershell) { //... } // Even after disposing of the PowerShell object, we still // need to set the…
noonand
  • 2,763
  • 4
  • 26
  • 51
14
votes
7 answers

Static disposable objects

How should I manage static classes with disposable items? Are there any rules of thumb? Basically, should I refactor and make the following DisposableDataManager class non- static or is it fine to leave everything to GC? . public static class…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
13
votes
1 answer

Calling Dispose on an BlockingCollection

I've reused the example producer consumer queue from the C# in a Nutshell book of Albahari (http://www.albahari.com/threading/part5.aspx#_BlockingCollectionT) and a colleague remarked: "Why isn't the Dispose called on the BlockingCollection in the…
Sander
  • 616
  • 1
  • 6
  • 17
13
votes
3 answers

How to check if ManualResetEvent has been disposed, when trying to Set() it inside an EventHandler?

I have the following design pattern: var myObjectWithEvents = new ObjectWithEvents(); using (var mre = new ManualResetEvent(false)) { var onEvent = new EventHandler((sender, e) => { mre.Set(); }); try { …
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126
13
votes
5 answers

To Dispose or not to Dispose (CA2000)

I'm switching on Code Analysis on an older project. Most remarks that result I can understand, but the CA2000: Dispose objects before losing scope is hard to get right. For instance, this code from an ASP.Net page: private void BuildTable() { …
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
13
votes
2 answers

SignalR dispose of HubConnection

In a AspNet SignalR client is it the action to dispose of a HubConnection necessary? It seems to take some time, from what I have seen...
cangosta
  • 1,174
  • 2
  • 13
  • 27
13
votes
11 answers

Why is 'using' improving C# performances

It seems that in most cases the C# compiler could call Dispose() automatically. Like most cases of the using pattern look like: public void SomeMethod() { ... using (var foo = new Foo()) { ... } // Foo isn't use after…
Wernight
  • 36,122
  • 25
  • 118
  • 131
13
votes
6 answers

When is Dispose necessary?

When you have code like: Bitmap bmp = new Bitmap ( 100, 100 ); Graphics g = Graphics.FromImage ( bmp ); Pen p = new Pen ( Color.FromArgb ( 128, Color.Blue ), 1 ); Brush b = new SolidBrush ( Color.FromArgb ( 128, Color.Blue ) ); g.FillEllipse ( b,…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
12
votes
1 answer

Why do I have to cast to a specific pointer type before calling Dispose?

Let's suppose I have an instance of the TList class (BDS 2006, so this is a list of pointer types). Each pointer I put into the list references memory allocated by the New() function. So when I want to clear the list, I have to iterate through it…
Mariusz Schimke
  • 3,185
  • 8
  • 45
  • 63