dispose

Dispose on nested Disposable items?

瘦欲@ 提交于 2019-12-01 19:02:22
I wanted to know if there are any conventions regarding disposal of disposable items nested inside another disposable item(in a property/public field, not as private members). For example, a DataSet contains DataTable(s) and a SqlCommand contains a SqlConnection. The obvious thing would be for a class to dispose of all Disposable items it owns, and leave the rest. Does there exist such a convention? If it does, how does .NET libraries determine who owns what? How can I find out whether nested objects are being disposed? PS: I have been wondering about this for a while, and apparently so have

How can I ensure that I dispose of an object in my singleton before the application closes?

喜欢而已 提交于 2019-12-01 18:14:28
问题 I'm using WatiN for some automated tests and what I found was that creating an IE instance for every test was not scalable. The creation and shutdown time of each IE instance was eating me alive: [TestMethod] public void Verify_Some_Useful_Thing() { using (var browser = new IE()) { browser.GoTo("/someurl"); // etc.. // some assert() statements } } However, the using statement did prove useful in that I can always ensure that my IE instance would have its dispose() method called which would

Return an object created by USING

邮差的信 提交于 2019-12-01 17:31:21
I am creating an object(obj below) in using and return that object as part of the function return.Will this cause any problem like object will be disposed before I try to use returned value in another function ? using (MyObject obj = new MyObject()) { . . . return obj; } Will this cause any problem like object will be disposed before I try to use returned value in another function? Yes. Can you explain what you're trying to do here? This code doesn't make any sense. The whole point of "using" is that you are using the object here only and then automatically getting rid of its scarce unmanaged

Do I need to call Graphics.Dispose()?

ぃ、小莉子 提交于 2019-12-01 17:20:39
In a VB.NET program I'm creating a new bitmap image, I then call Graphics.FromImage to get a Graphics object to draw on the bitmap. The image is then displayed to the user. All the code samples I've seen always call .Dispose() on Bitmaps and Graphics objects, but is there any need to do that when neither have touched files on disk? Are there any other unmanaged resources that these objects might have grabbed that wouldn't be cleared by the garbage collector? Yes. Always call Dispose() on any object that implements IDisposable . GDI handles used by graphics objects are unmanaged and require

How does the IDisposable interface work?

萝らか妹 提交于 2019-12-01 16:02:51
I understand that it is used to deallocate unmanaged resources, however, I am confused as to when Dispose is actually called. I know it is called at the end of a using block, but does it also get invoked when the object is garbage collected? If you implement IDisposable correctly, you should also include a finalizer that will call Dispose() on your object. If you do that, it will get called by the GC. However, it's still a VERY good idea to try to always dispose of these objects yourself. The biggest problem with relying on the finalizer to call Dispose is that it will happen in another thread

Why does the traditional Dispose pattern suppress finalize?

早过忘川 提交于 2019-12-01 15:49:19
Assuming this as the traditional Dispose pattern (taken from devx but seen on many websites) class Test : IDisposable { private bool isDisposed = false; ~Test() { Dispose(false); } protected void Dispose(bool disposing) { if (disposing) { // Code to dispose the managed resources of the class } // Code to dispose the un-managed resources of the class isDisposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } } I don't understand why we call GC.SupressFinalize(this) . This requires me to write my own managed resource disposal, including nulling my references? I'm a

Why does the traditional Dispose pattern suppress finalize?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 14:50:51
问题 Assuming this as the traditional Dispose pattern (taken from devx but seen on many websites) class Test : IDisposable { private bool isDisposed = false; ~Test() { Dispose(false); } protected void Dispose(bool disposing) { if (disposing) { // Code to dispose the managed resources of the class } // Code to dispose the un-managed resources of the class isDisposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } } I don't understand why we call GC.SupressFinalize(this

How does the IDisposable interface work?

大城市里の小女人 提交于 2019-12-01 14:50:39
问题 I understand that it is used to deallocate unmanaged resources, however, I am confused as to when Dispose is actually called. I know it is called at the end of a using block, but does it also get invoked when the object is garbage collected? 回答1: If you implement IDisposable correctly, you should also include a finalizer that will call Dispose() on your object. If you do that, it will get called by the GC. However, it's still a VERY good idea to try to always dispose of these objects yourself

Java Disposable pattern

孤者浪人 提交于 2019-12-01 13:58:40
问题 C# supports disposable pattern for deterministic garbage collection using the dispose pattern. Is there such pattern for java? Java 7 has autoclosable, which you can use with try finally blocks to invoke the close method. What about versions prior to 7? Is there a disposable pattern (deterministic garbage collection) for Java 5 or 6? 回答1: The closest prior to Java 7 is just "manual" try/finally blocks: FileInputStream input = new FileInputStream(...); try { // Use input } finally { input

Do IDisposable objects get disposed of if the program is shut down unexpectedly?

别等时光非礼了梦想. 提交于 2019-12-01 13:43:00
问题 What happens if the program exits unexpectedly (either by exception or the process is terminated)? Are there any situations like this (or otherwise) where the program will terminate, but IDisposable objects won't be properly disposed of? The reason I'm asking is because I'm writing code that will communicate with a peripheral, and I want to make sure there's no chance it will be left in a bad state. 回答1: If the cause is an exception and thrown from within a using block or a try catch finally