dispose

How to dispose managed resource in Dispose() method in C#?

亡梦爱人 提交于 2019-11-27 17:50:57
问题 I know Dispose() is intended for unmanaged resource, and the resource should be disposed when it is no longer needed without waiting for the garbage collector to finalize the object. However, when disposing the object, it suppress finalization of the garbage collector (GC.SuppressFinalize(this); in the code below). This means that if the object includes managed resource, we will have to take care of that too because garbage collector will not clean this up. In the example code below (from

Purpose of Dispose calling Dispose(IsDisposing) pattern in C#?

巧了我就是萌 提交于 2019-11-27 16:44:14
问题 Here is code from MSDN. I don't understand why the work isn't just done in the regular Dispose() method here. What is the purpose of having the Dispose(bool) method? Who would ever call Dispose(false) here? public void Dispose() { Dispose(true); // Use SupressFinalize in case a subclass // of this type implements a finalizer. GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { // If you need thread safety, use a lock around these // operations, as well as in your

Any sense to set obj = null(Nothing) in Dispose()?

♀尐吖头ヾ 提交于 2019-11-27 15:32:25
问题 Is there any sense to set custom object to null ( Nothing in VB.NET) in the Dispose() method? Could this prevent memory leaks or it's useless?! Let's consider two examples: public class Foo : IDisposable { private Bar bar; // standard custom .NET object public Foo(Bar bar) { this.bar = bar; } public void Dispose() { bar = null; // any sense? } } public class Foo : RichTextBox { // this could be also: GDI+, TCP socket, SQl Connection, other "heavy" object private Bitmap backImage; public Foo

Finalizer and IDisposable

China☆狼群 提交于 2019-11-27 14:47:51
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 the object), and you dont have any unmanaged resources to clean up? As I see it, if the class has only managed resources and if you dont call Dispose, the managed resources will automatically get cleaned up by the GC and hence no need to implement the finalizer. Am I wrong? Also, what if I am using my Dispose method to clean up event handlers. As

Who Disposes of an IDisposable public property?

此生再无相见时 提交于 2019-11-27 14:17:40
If I have a SomeDisposableObject class which implements IDisposable : class SomeDisposableObject : IDisposable { public void Dispose() { // Do some important disposal work. } } And I have another class called AContainer , which has an instance of SomeDisposableObject as a public property: class AContainer { SomeDisposableObject m_someObject = new SomeDisposableObject(); public SomeDisposableObject SomeObject { get { return m_someObject; } set { m_someObject = value; } } } Then FxCop will insist that AContainer is also made IDisposable . Which is fine, but I can't see how I can safely call m

Is it necessary to dispose System.Timers.Timer if you use one in your application?

与世无争的帅哥 提交于 2019-11-27 13:03:31
I am using System.Timers.Timer class in one of the classes in my application. I know that Timer class has Dispose method inherited from the parent Component class that implements IDisposable interface. Instances of the class below are created many times during my application lifecycle; each of them has an instance of Timer class that generates Elapsed events continuously during the class's lifecycle. Should I implement IDisposable interface in the class that uses Timer class to dispose the timer object? (I have seen code that doesn't do this at all). I am afraid that some unmanaged resources

How do I extend a WinForm's Dispose method?

谁说我不能喝 提交于 2019-11-27 12:58:50
I am getting this warning from FxCop: "'RestartForm' contains field 'RestartForm.done' that is of IDisposable type: 'ManualResetEvent'. Change the Dispose method on 'RestartForm' to call Dispose or Close on this field." Ok, I understand what this means and why this is what needs to be done... Except System.Windows.Forms.Form doesn't allow you to override either .Close() or .Dispose() , so what to do? Currently I'm running with this solution: private void RestartForm_FormClosing(object sender, FormClosingEventArgs e) { done.Set(); done.Close(); } Which works as intended for my application...

Best way to dispose a list

可紊 提交于 2019-11-27 12:47:32
问题 I am having List object. How can I dispose of the list? For example, List<User> usersCollection =new List<User>(); User user1 = new User(); User user2 = new User() userCollection.Add(user1); userCollection.Add(user2); If I set userCollection = null; what will happen? foreach(User user in userCollection) { user = null; } Which one is best? 回答1: Best idea is to leave it to the garbage collector. Your foreach will do nothing since only the reference will be set to null not the element in the

When should I dispose my objects in .NET?

六月ゝ 毕业季﹏ 提交于 2019-11-27 12:09:41
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? Dispose of an object the instant your are done with it. Disposable objects represent objects holding a valuable resource which the CLR is not intrinsically aware of. Consequently the GC is also unaware of the resources and is unable to make intelligent decisions as to when it should collect a disposable object and hence free the underlying resource. Eventually the GC will feel memory pressure and collect

Is there a situation in which Dispose won't be called for a 'using' block?

痴心易碎 提交于 2019-11-27 11:51:22
问题 This was a telephone interview question I had: Is there a time when Dispose will not be called on an object who's scope is declared by a using block? My answer was no - even if an exception happens during the using block, Dispose will still be called. The interviewer disagreed and said if using is wrapped in a try - catch block then Dispose will not be called by the time you enter the catch block. This goes contrary to my understanding of the construct, and I haven't been able to find