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

Check if Stateless widget is disposed in flutter

When my stateless widget built I play some sounds in sequence order by using this code: await _audioPlayer.play(contentPath1, isLocal: true); await Future.delayed(Duration(seconds: 4)); await _audioPlayer.play(contentPath2, isLocal: true); await…
Flutter IO Dev
  • 1,759
  • 5
  • 15
  • 20
16
votes
2 answers

Idiomatic Clojure way to spawn and manage background threads

What is the idiomatic Clojure way to create a thread that loops in the background doing updates to some shared refs and to manage its lifetime? I find myself using future for this, but it feels like a little bit of a hack as I never return a…
pauldoo
  • 18,087
  • 20
  • 94
  • 116
16
votes
3 answers

Two questions about Dispose() and destructors in C#

I have a question about how to use Dispose() and destructors. Reading some articles and the MSDN documentation, this seems to be the recommended way of implementing Dispose() and destructors. But I have two questions about this implementation, that…
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
16
votes
5 answers

Dispose form after closing

I have got the new problem with opening and closing form in C#. My problem is how to dispose the form after closing . here is my code : Program.cs: static class Program { public static Timer timer; [STAThread] static void Main() { …
Mironline
  • 2,755
  • 7
  • 35
  • 61
16
votes
4 answers

CA2213 code analysis rule and auto-implemented properties

I use static code analysis in our projects to check for code violations. One of extensively used rules is CA2213, which checks for correct disposing of disposable fields. I noticed CA2213 does not check disposing of auto implemented properties.…
Zvonko
  • 363
  • 2
  • 19
16
votes
2 answers

How to dispose properly using async and await

I'm trying to make code replacement from Thread to Task. The sleep / delay is just representing long running activity. static void Main(string[] args) { ThreadDoWork(); TaskDoWork(); } public static void ThreadDoWork() { using (var…
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
16
votes
6 answers

Do I need to call Close() on a ManualResetEvent?

I've been reading up on .NET Threading and was working on some code that uses a ManualResetEvent. I have found lots of code samples on the internet. However, when reading the documentation for WaitHandle, I saw the following: WaitHandle…
Kevin Hakanson
  • 41,386
  • 23
  • 126
  • 155
16
votes
2 answers

Do custom events need to be set to null when disposing an object?

Lets says we have 2 objects, Broadcaster and Listener. Broadcaster has an event called Broadcast to which Listener is subscribed. If Listener is disposed without unsubscribing from the Broadcast event it will be retained in memory because of the…
Dan Rigby
  • 17,133
  • 6
  • 43
  • 60
16
votes
2 answers

Execute code when VisualStudio debugger is exiting

I had assumed that when terminating debugging (such as by hitting the Stop button, or hitting Shift+F5), that any class implementing a finalizer or IDisposable would be, well, disposed. I have some classes that implement IDisposable. There are a…
CoolUserName
  • 3,715
  • 6
  • 26
  • 30
16
votes
9 answers

Is there a list of common object that implement IDisposable for the using statement?

I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection, MemoryStream, etc. Taking it one step further, it would be great to even show the other "pieces of the puzzle", like how you…
John B
  • 20,062
  • 35
  • 120
  • 170
15
votes
2 answers

What is the difference between finalize and dispose in .net?

Possible Duplicate: Finalize vs Dispose Hi, Recently I was quizzed in an interview about finalize and dispose. When is each one of them used and how is Garbage Collector related to them. Kindly share links to enlighten more on the topic. Kindly…
HotTester
  • 5,620
  • 15
  • 63
  • 97
15
votes
6 answers

Try/Finally block vs calling dispose?

Is there any difference between these two code samples and if not, why does using exist? StreamWriter writer; try { writer = new StreamWriter(...) writer.blahblah(); } finally { writer.Dispose(); } vs: using (Streamwriter writer = new…
rollsch
  • 2,518
  • 4
  • 39
  • 65
15
votes
4 answers

Should I dispose a BinaryReader if I need to preserve the "wrapped" stream?

Both BinaryReader constructors require a stream parameter. If I need to keep the underlying stream as-is when I'm done with the BinaryReader, should I still call its Dispose()? If not, is there any other clean-up for the no longer needed…
user610650
14
votes
2 answers

Safely disposing Excel interop objects in C#?

i am working on a winforms c# visual studio 2008 application. the app talks to excel files and i am using Microsoft.Office.Interop.Excel; to do this. i would like to know how can i make sure that the objects are released even when there is an…
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
14
votes
4 answers

Problems solving "Cannot access disposed object." exception

In my current project there is a Form class which looks like this: public partial class FormMain : Form { System.Timers.Timer timer; Point previousLocation; double distance; public FormMain() { InitializeComponent(); …
haiyyu
  • 2,194
  • 6
  • 22
  • 34