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
20
votes
8 answers

When should I dispose my objects in .NET?

For general code, do I really need to dispose an object? Can I just ignore it for the most part or is it a good idea to always dispose an object when your 100% sure you don't need it anymore?
danmine
  • 11,325
  • 17
  • 55
  • 75
20
votes
3 answers

How to delete the file that was sent as StreamContent of HttpResponseMessage

In ASP.NET webapi, I send a temporary file to client. I open a stream to read the file and use the StreamContent on the HttpResponseMessage. Once the client receives the file, I want to delete this temporary file (without any other call from the…
Bhanu Gotluru
  • 235
  • 2
  • 7
20
votes
8 answers

SQLite.Interop.dll locked after running Visual Studio 2012 Unit Test Framework tests

Visual Studio 2012 SQLite 1.0.82.0 (from nuget) I am trying to use the "Run All" command in the "Test Explorer" The following error happens after you run the test once ... after that it will not build anymore, until you restart visual studio Here…
ObeseCoder
  • 391
  • 2
  • 6
19
votes
3 answers

Java Disposable pattern

C# supports disposable pattern for deterministic garbage collection using the dispose pattern. Is there such pattern for java? Java 7 has autoclosable, which you can use with try finally blocks to invoke the close method. What about versions prior…
DarthVader
  • 52,984
  • 76
  • 209
  • 300
19
votes
4 answers

Why is use better than using?

According to the last sentence on this MSDN page use is to be preferred over using. I've heard it elsewhere (this answer, for example). Why is this? I realize use was added later. But what's the difference? On the surface, using seems more useful…
Daniel
  • 47,404
  • 11
  • 101
  • 179
18
votes
4 answers

Does one need to close both NetworkStream and TcpClient, or just TcpClient?

I'm reading the documentation on TcpClient.Close() and noticed this: Calling this method will eventually result in the close of the associated Socket and will also close the associated NetworkStream that is used to send and receive data if one was…
Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136
18
votes
1 answer

Do I need to dispose of a Task?

I am having fun working with System.Threading.Tasks. Many of the code samples I see, however, look something like so: Dim lcTask = Task.Factory.StartNew(Sub() DoSomeWork()) Dim lcTaskLong = Task.Factory.StartNew(Sub() DoSomeWork(),…
Kevin Buchan
  • 2,790
  • 3
  • 27
  • 34
18
votes
3 answers

Datatable.Dispose() will make it remove from memory?

I have researching through very simple code and get stuck on seeing the dispose() result of datatable Following is the code DataTable dt= new Datatable(); SqlCommand Cmd = new SqlCommand("sp_getData",SqlCon); SqlCommand.CommandType=…
Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83
18
votes
2 answers

Entity Framework - How should I instance my "Entities" object

I'm a total newbie at Entity Framework and ASP.Net MVC, having learned mostly from tutorials, without having a deep understanding of either. (I do have experience on .Net 2.0, ADO.Net and WebForms) My current doubt comes from the way I'm instancing…
Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
18
votes
8 answers

SPWeb.Site, should you call Dispose() on it?

Updated 06/08/2009 15:52: Short answer NO. Original question: I can't find any reference which gives guidance on SPWeb.Site regarding disposing. I've gone through some of the more popular best practises documentation on disposing SharePoint…
Edward Wilde
  • 25,967
  • 8
  • 55
  • 64
17
votes
6 answers

Why disposed object doesn't throw exception on using it after disposing?

Is it legal to call a method on disposed object? If yes, why? In the following demo program, I've a disposable class A (which implements IDisposable interface).As far as I know, if I pass disposable object to using() construct, then Dispose() method…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
17
votes
3 answers

Should I dispose a Mutex?

I'm working on 2 Windows Services that have a common database which I want to lock (cross-process) with a system Mutex. Now I'm wondering whether it's ok to just call WaitOne() and ReleaseMutex() in a try-finally block or should I also dispose the…
Koen
  • 3,626
  • 1
  • 34
  • 55
17
votes
6 answers

Need I remove controls after disposing them?

.NET 2 // dynamic textbox adding myTextBox = new TextBox(); this.Controls.Add(myTextBox); // ... some code, finally // dynamic textbox removing myTextBox.Dispose(); // this.Controls.Remove(myTextBox); ?? is this needed Little…
serhio
  • 28,010
  • 62
  • 221
  • 374
17
votes
1 answer

Dispose a dictionary in C# best practice

I have a ConcurrentDictionary in my session class. Key is an interface representing a manager class. Value is a List of DataContracts classes that are used for that manager in this session. When I dispose the session class, I wish to clear this…
ilansch
  • 4,784
  • 7
  • 47
  • 96
16
votes
7 answers

How to dispose objects having asynchronous methods called?

I have this object PreloadClient which implements IDisposable, I want to dispose it, but after the asynchronous methods finish their call... which is not happening private void Preload(SlideHandler slide) { using(PreloadClient client…
Jalal El-Shaer
  • 14,502
  • 8
  • 45
  • 51