dispose

Identify IDisposable objects

戏子无情 提交于 2019-11-30 09:01:19
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 objects declared in. I mean something like resharper or another visual studio plugin. thanks. Neil Barnwell I know what you mean. I don't know, but look at FxCop. It might have a rule in there somewhere that checks whether objects implementing IDisposable are not disposed. Just a hunch, mind. UPDATE : Mitch Wheat writes: FxCop includes the rule,

Why call Dispose() before main() exits?

久未见 提交于 2019-11-30 08:27:07
问题 My .net service cleans up all its unmanaged resources by calling resourceName.Dispose() in a finally block before the Main() loop exits. Do I really have to do this? Am I correct in thinking that I can’t leak any resources because the process is ending? Windows will close any handles that are no longer being used, right? 回答1: There is no limit to the types of resources that may be encapsulated by an object implementing IDisposable . The vast majority of resources encapsulated by IDisposable

When do I need to use dispose() on graphics?

纵饮孤独 提交于 2019-11-30 08:23:33
I'm learning to draw stuff in C# and I keep seeing recommendations to use dispose(), but I don't quite understand what it does. When should I be using dispose() on a code-drawn graphic? What happens if I don't? Do I need to call it every time a graphic is not visible, such as on a GUI that has tabs and the user switched to the other tab, and then redraw it when they switch back? Will I break things if I call it when I shouldn't? Will Batman escape the evil clutches of the Joker? When should I be using dispose() on a code-drawn graphic? Whenever you are completely done with any object that

Try/Finally block vs calling dispose?

放肆的年华 提交于 2019-11-30 07:18:15
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 Streamwriter(...)) { writer.blahblah } I mean in the second example you really should be putting it in a try block anyway so adding the finally block really doesn't use much more effort. I get that the entire thing might be encompassed in a bigger try block but yeah, just seems superfluous to me. There're some issues with your code. Never write like this (put

Why should Dispose() be non-virtual?

时光怂恿深爱的人放手 提交于 2019-11-30 05:43:48
I'm new to C#, so apologies if this is an obvious question. In the MSDN Dispose example , the Dispose method they define is non-virtual. Why is that? It seems odd to me - I'd expect that a child class of an IDisposable that had its own non-managed resources would just override Dispose and call base.Dispose() at the bottom of their own method. Thanks! Typical usage is that Dispose() is overloaded, with a public, non-virtual Dispose() method, and a virtual, protected Dispose(bool). The public Dispose() method calls Dispose(true), and subclasses can use this protected virtual method to free up

Causing a UserControl to remove itself (WPF)

风流意气都作罢 提交于 2019-11-30 05:34:04
问题 In winforms I usually do Parent.Controls.Remove(this); to have a UserControl remove itself. This isn't working for wpf. My control has button on it to remove the whole UserControl, any ideas how to accomplish this in wpf? Thanks in advance 回答1: You will need to know the type of the Parent property to remove yourself from your Parent control. All Panel type parents (Grid, WrapPanel, StackPanel) have the Children property: i.e. for Grid: ((Grid)button.Parent).Children.Remove(this);

(.net) CriticalFinalizerObject - What does it really do?

一世执手 提交于 2019-11-30 05:15:12
问题 My understanding about this class is that you should use it when you want to be sure that the Finalizer(destructor) or the class is called, but from a couple of tests I did, it doesn't seem to be true. If it does not make sure that the dispose method is called, is there any other way of doing it? For example, if i want to make sure that some code is run to end my object, even if I close my program via Task Manager or something? 回答1: Chris Brumme has explained this topic in a way I'm not sure

What's the purpose of the components IContainer generated by the Winforms designer?

这一生的挚爱 提交于 2019-11-30 04:45:25
When you create a new form in Visual Studio, the designer generates the following code in the .Designer.cs file: /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } What is the purpose of the components variable? My

Proper cleanup of WPF user controls

别来无恙 提交于 2019-11-30 04:44:19
I am relatively new to WPF, and some things with it are quite foreign to me. For one, unlike Windows Forms, the WPF control hierarchy does not support IDisposable. In Windows Forms, if a user control used any managed resources, it was very easy to clean up the resources by overriding the Dispose method that every control implemented. In WPF, the story is not that simple. I have searched for this for several hours, and encountered two basic themes: The first theme is Microsoft clearly stating that WPF does not implement IDisposable because the WPF controls have no unmanaged resources. While

Declare IDisposable for the class or interface?

不打扰是莪最后的温柔 提交于 2019-11-30 04:12:26
Starting from the follwing situation: public interface ISample { } public class SampleA : ISample { // has some (unmanaged) resources that needs to be disposed } public class SampleB : ISample { // has no resources that needs to be disposed } The class SampleA should implement the interface IDisposable for releasing resources. You could solve this in two ways: 1. Add the required interface to the class SampleA: public class SampleA : ISample, IDisposable { // has some (unmanaged) resources that needs to be disposed } 2. Add it to the interface ISample and force derived classes to implement it: