dispose

Finalizers and Dispose

ぃ、小莉子 提交于 2019-11-29 15:50:59
问题 I've got a class named BackgroundWorker that has a thread constantly running. To turn this thread off, an instance variable named stop to needs to be true . To make sure the thread is freed when the class is done being used, I've added IDisposable and a finalizer that invokes Dispose() . Assuming that stop = true does indeed cause this thread to exit, is this sippet correct? It's fine to invoke Dispose from a finalizer, right? Finalizers should always call Dispose if the object inherits

Memory Leak questions

可紊 提交于 2019-11-29 15:34:38
I've been reading a lot about this since I've been asked to fix a C# application that has memory leaking problems, but I haven't found an answer for these 2 issues: Consider the following code: private static ArrayList list = new ArrayList(); public void Function() { list.add(object1); list.add(object2); //didn't call clear() prior to reusing list list = new ArrayList(); } Since the list wasn't cleared before creating a new one, will this generate some sort of garbage that won't be released after the static list itself is disposed? The second issue is regarding Form.Dispose(). I see that a lot

Why “Finalize method should not reference any other objects”?

寵の児 提交于 2019-11-29 14:07:46
I have been pondering why it is recommended that we should not release managed resources inside finalize. If you see the code example at http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx , and search for string "Dispose(bool disposing) executes in two distinct scenarios" and read that comment, you will understand what I mean. Only possibility I can think of is that it probably has something to do with the fact that it is not possible to predict when finalizer will get called. Does anyone know the right answer ? thanks, mishal Jon Skeet If you're referencing another object

Will a using clause close this stream?

情到浓时终转凉″ 提交于 2019-11-29 13:07:23
I've apparently worked myself into a bad coding habit. Here is an example of the code I've been writing: using(StreamReader sr = new StreamReader(File.Open("somefile.txt", FileMode.Open))) { //read file } File.Move("somefile.txt", "somefile.bak"); //can't move, get exception that I the file is open I thought that because the using clause explicitly called Close() and Dispose() on the StreamReader that the FileStream would be closed as well. The only way I could fix the problem I was having was by changing the above block to this: using(FileStream fs = File.Open("somefile.txt", FileMode.Open))

Identify IDisposable objects

霸气de小男生 提交于 2019-11-29 12:15:03
问题 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. 回答1: 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

Windows Form Fonts Questions Part 1

心已入冬 提交于 2019-11-29 11:12:42
I have some user controls in a windows form. I wonder if i set the Font property of the main form, will its child get a) a copy of the new Font , or b) a reference to the new Font , or c) nothing? Does a font need to be disposed? For example, can I do the following code safely? form.Font = new Font(...); Will a font get disposed automatically when the parent ( Form or UserControl ) is disposed? Thanks, Gilbert Both. The Font property is its own .NET object. Winforms however caches the native Windows font, they are fairly expensive to create. The .NET wrapper object is quite small. Yes. The

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

牧云@^-^@ 提交于 2019-11-29 11:08:57
问题 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? 回答1: When

MemoryStream must be explicitely be disposed?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 10:50:07
As MemoryStream is an unmanaged resource does it always have to be disposed? Given: 1) A method is invoked. 2) A MemoryStream object is created (MemoryStream ms = new MemoryStream();). 3) An exception occurs and is caught from the invoking classes. The reference on the MemoryStream object is therefore lost. Does this scenario need a try/finally-block (or using-statement)? In general, all disposable objects must always be disposed. However, MemoryStream doesn't actually need to be disposed, since it doesn't have any unmanaged resources. (It's just a byte[] and an int ) The only reason it's

Does SqlTransaction need to have Dispose called?

老子叫甜甜 提交于 2019-11-29 10:25:55
Do I need to call dispose in the finally block for SqlTransaction? Pretend the developer didnt use USING anywhere, and just try/catch. SqlTransaction sqlTrans = con.BeginTransaction(); try { //Do Work sqlTrans.Commit() } catch (Exception ex) { sqlTrans.Rollback(); } finally { sqlTrans.Dispose(); con.Dispose(); } Tim Schmelter Do I need to use try-finally or the using -statement to dispose the SqlTransaction ? It does not hurt to have it. This is true for every class implementing IDisposable , otherwise it would not implement this interface. But normally the garbage collector would deal with it

How to get notified before static variables are finalized

时光毁灭记忆、已成空白 提交于 2019-11-29 09:55:59
When can i cleanup objects stored in static variables in C#? I have a static variable that is lazily initialized : public class Sqm { private static Lazy<Sqm> _default = new Lazy<Sqm>(); public static Sqm Default { get { return _default.Value; } } } Note : That i've just changed Foo to be a static class. It doesn't change the question in any way if Foo is static or not. But some people are convinced that there is no way that an instance of Sqm could be constructed without first constructing an instance of Foo . Even if i did create a Foo object; even if i created 100 of them, it wouldn't help