dispose

Using Dispose on a Singleton to Cleanup Resources

主宰稳场 提交于 2019-11-30 03:57:22
问题 The question I have might be more to do with semantics than with the actual use of IDisposable . I am working on implementing a singleton class that is in charge of managing a database instance that is created during the execution of the application. When the application closes this database should be deleted. Right now I have this delete being handled by a Cleanup() method of the singleton that the application calls when it is closing. As I was writing the documentation for Cleanup() it

Should Dispose methods be unit tested?

那年仲夏 提交于 2019-11-30 00:51:44
问题 I am using C#. Is it advised to unit test dispose methods? If so why, and how should one test these methods? 回答1: Yes, but it might be hard. There are two things that can generally happen in Dispose implementation: Unmanaged resources are released. In this case it's pretty hard to verify that the code called, for example, Marshal.Release . A possible solution is to inject an object that can do the disposing and pass a mock to it during testing. Something to this effect: interface

How to handle exception thrown from Dispose?

二次信任 提交于 2019-11-30 00:38:10
问题 Recently, I was researching some tricky bugs about object not disposed. I found some pattern in code. It is reported that some m_foo is not disposed, while it seems all instances of SomeClass has been disposed. public class SomeClass: IDisposable { void Dispose() { if (m_foo != null) { m_foo.Dispose(); } if (m_bar != null) { m_bar.Dispose(); } } private Foo m_foo; private Bar m_bar; } I suspects that Foo.Dispose might throw a exception, so that following code is not executed so m_bar is not

How do I dispose of resources after an ASP.NET Web API request completes?

喜欢而已 提交于 2019-11-29 23:46:48
问题 I am attempting to expose an IQueryable<> over ASP.NET Web API and I find that I need to keep the data source open until the request completes, so that the OData query system built into ASP.NET Web API can do its job. OK, that sounds perfectly reasonable. But where do I dispose of the data source? I do not see any obvious place for this. Should I manage request state in the Application? What is the standard way to do this? Is the Dispose() method of the controller the appropriate place? I.e.

How do I add Dispose functionality to a C# UserControl?

女生的网名这么多〃 提交于 2019-11-29 23:09:50
I have a class which implements UserControl. In .NET 2005, a Dispose method is automatically created in the MyClass.Designer.cs partial class file that looks like this: protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } If I want to add my own Dispose functionality, where would I put it? Since this file is generated, I don't want to add code here and risk it getting blown away. In such a case I move the generated Dispose method to the main file and extend it. Visual Studio respects this. An other

Handling with temporary file stream

微笑、不失礼 提交于 2019-11-29 20:10:02
问题 Say I want to define a TempFileStream class that creates a temporary file using Path.GetTempFileName() method. A temporary file must be deleted when TempFileStream's object is no longer needed, e.g. closed or disposed: class TempFileStream: FileStream { string m_TempFileName = Path.GetTempFileName(); public TempFileStream(FileMode fileMode): base(m_TempFileName,fileMode) {} /// ... public ovverride Dispose(bool disposing) { /// ??? } } How should I implement this simply and safely? 回答1: Try

Why does Code Analysis tell me, “Do not dispose objects multiple times” here:

允我心安 提交于 2019-11-29 16:34:47
On this code: public static string Base64FromFileName(string fileName) { try { FileInfo fInfo = new FileInfo(fileName); long numBytes = fInfo.Length; FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fStream); byte[] bdata = br.ReadBytes((int)numBytes); br.Close(); fStream.Close(); return Convert.ToBase64String(bdata); } catch(Exception e) { throw e; } } ...I get, courtesy of Visual Studio's Code Analysis tool, the warning, " Do not dispose objects multiple times...To avoid generating a System.ObjectDisposedException you should

How to dispose asynchronously?

与世无争的帅哥 提交于 2019-11-29 16:32:54
问题 Let's say I have a class that implements the IDisposable interface. Something like this: MyClass uses some unmanaged resources, hence the Dispose() method from IDisposable releases those resources. MyClass should be used like this: using ( MyClass myClass = new MyClass() ) { myClass.DoSomething(); } Now, I want to implement a method that calls DoSomething() asynchronously. I add a new method to MyClass : Now, from the client side, MyClass should be used like this: using ( MyClass myClass =

Disposing JFrame by clicking from an inner JPanel

♀尐吖头ヾ 提交于 2019-11-29 16:25:45
I'm trying to dispose my JFrame by clicking a button, located on a JPanel that is placed on the JFrame that I want to close. I tried to make a static method on the JFrame class, but ofcourse my IDE told me that wasn't going to happen. Anyone thinking of a solution? Thanks! Try this: public class DisposeJFrame extends JFrame{ JPanel panel = new JPanel(); JButton button = new JButton("Dispose JFrame"); public DisposeJFrame(){ super(); setTitle("Hi"); panel.add(button); add(panel); pack(); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) {

Does the “using” keyword mean the object is disposed and GC'ed?

白昼怎懂夜的黑 提交于 2019-11-29 15:56:11
问题 I struck up a conversation with my colleague today, who said she'd just learned the reason behind using the using statement. //Using keyword is used to clean up resources that require disposal (IDisposable interface). using (StreamReader reader = new StreamReader(@"C:\test.txt")) { string line = reader.ReadLine(); } I pointed out that the object is marked as "Can be disposed" but not actually disposed and garbage-collected unless GC decides to do so. She responded that the object will be