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
11
votes
7 answers

Dispose question

I have a number of classes which have private member variables that implement IDisposable (timers, brushes, etc). Do I need to do anything to ensure these variables are cleaned up properly by the .NET Framework? The literature I've come across is…
Trevor Balcom
  • 3,766
  • 2
  • 32
  • 51
11
votes
4 answers

When do we need to call Dispose() in dot net c#?

Do I need to dispose a sqldatareader after it is created? SqlDataReader reader; --- --- --- reader.Close(); reader.Dispose();
Paras
  • 2,997
  • 6
  • 35
  • 46
11
votes
2 answers

Dispose SmtpClient in SendComplete?

When I use SmtpClient's SendAsync to send email, how do I dispose the smtpclient instance correctly? Let's say: MailMessage mail = new System.Net.Mail.MailMessage() { Body = MailBody.ToString(), IsBodyHtml = true, From = new…
Jeff Chen
  • 736
  • 1
  • 8
  • 20
11
votes
1 answer

Why is Flutter disposing my widget state object in a tabbed interface?

I have a tabbed Flutter interfaces using DefaultTabController with 3 pages, each a stateful widget. I seem to be able to switch between the first two tabs just fine, but when I tab to the 3rd page the state object for the first page gets disposed.…
Keith A
  • 133
  • 1
  • 6
11
votes
1 answer

Should one dispose of the WebResponse reference in WebException, if raised from WebClient?

Related question: WebClient in .Net not releasing socket resources While debugging a resource leak issue, I noticed that System.Net.WebException (a non-disposable type) contains a reference to System.Net.WebResponse (a disposable type). I am…
Steve Guidi
  • 19,700
  • 9
  • 74
  • 90
11
votes
3 answers

When will an object declared in a static class get garbage collected?

public static class stClass { static Class1 obj = new Class1(); public static int returnSomething() { return 0; } } When will the Class1 instance obj in stClass get garbage collected, if i am calling the static function…
Vamsi
  • 4,237
  • 7
  • 49
  • 74
11
votes
12 answers

When to dispose and why?

I asked a question about this method: // Save an object out to the disk public static void SerializeObject(this T toSerialize, String filename) { XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); TextWriter…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
11
votes
6 answers

Finalizer and IDisposable

Based on the documentation (MSDN: link), it is clear that one should use the IDisposable pattern when implementing a finalizer. But do you need to implement a finalizer if you implement IDisposable (so as to provide a deterministic way of disposing…
Raj Rao
  • 8,872
  • 12
  • 69
  • 83
11
votes
1 answer

Adding / Removing components on the fly

I need to be able to add & remove Angular components on the fly. To do so, I'm using loadIntoLocation and dispose methods, like it: Adding a component (from a layout manager): this.m_loader.loadIntoLocation(MyComponent, this.m_element,…
Tim Autin
  • 6,043
  • 5
  • 46
  • 76
11
votes
2 answers

How to ensure that Autofac is calling Dispose() on EF6 DbContext

UPDATE Found this little gem which helped me with DbContext Josh Kodroff - Making Entity Framework More Unit-Testable Original After doing a lot of research I finally decided to implement IOC using Autofac in my MVC5 EF6 project. Autofac's…
SeanG80
  • 159
  • 1
  • 1
  • 10
11
votes
7 answers

return the variable used for using inside the using C#

I am returning the variable I am creating in a using statement inside the using statement (sounds funny): public DataTable foo () { using (DataTable properties = new DataTable()) { // do something return properties; …
di3go
  • 133
  • 1
  • 1
  • 5
11
votes
1 answer

Does GC collects garbage from Metaspace?

Always I thought that garbage collector clear only heap and now I think so. In java 8 permGen was deleted and it was replaced by Metaspace. And As I understood Metaspace is garbage collected(https://stackoverflow.com/a/24075360/2674303) Who collects…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
11
votes
6 answers

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

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…
Colin
  • 22,328
  • 17
  • 103
  • 197
11
votes
4 answers

Will all objects created inline in a `using` statement be disposed of?

This may be answered elsewhere, but after doing a bit of searching I didn't find much on the subject outside of the normal using context. I am curious if all objects created in a using block will be disposed of as well as the original object. Here…
Evan L
  • 3,805
  • 1
  • 22
  • 31
11
votes
4 answers

Throwing exception in finalizer to enforce Dispose calls:

Here is the typical IDisposable implementation that I believe is recommended: ~SomeClass() { Dispose(false); } public void Dispose() { GC.SuppressFinalize(this); Dispose(true); } protected virtual void Dispose(bool isDisposing) { …
Kelsie
  • 1,000
  • 1
  • 9
  • 21