dispose

When do I need to manage managed resources?

旧街凉风 提交于 2019-12-12 11:54:11
问题 I have been looking at the standard Dispose pattern and I'm just wondering what I need to write to free managed resources? If these resources are 'managed' already then surely I shouldn't need to do anything. If that's the case, and my class doesn't hold any unmanaged resources (hence no need for it to be finalized by GC) then do I only need to suppress finalization in my Dispose method? :- public void Dispose() { GC.SuppressFinalize(this); } so suppose this is my class: public sealed class

Do I need to dispose an HttpResponseException from Request.CreateResponse()?

南笙酒味 提交于 2019-12-12 10:39:02
问题 I have this code within a request handling method of an ApiController: if (uri != null) { HttpResponseMessage r = Request.CreateResponse(HttpStatusCode.Redirect); r.Headers.Location = uri; throw new HttpResponseException(r); } The potential problem is that "r" is never disposed (in my code at least). I could wrap this in a using, but then wouldn't "r" get disposed before the response is streamed to the client? What is the correct way to handle this? 回答1: All the examples I've seen show that

the correct technique for releasing a socket/event/ummaged code with the dispose/finalize pattern

依然范特西╮ 提交于 2019-12-12 10:35:21
问题 How to implement the Dispose pattern when my class contains a socket & event? Should it be something like this? class MyClass { Socket m_ListenerSocket = new Socket(); book m_Disposed=false; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool isDisposing) { if (!m_Disposed) { if (isDisposing) { if (m_ListenerSocket != null) { m_ListenerSocket.Dispose(); innerClass.Notify -= Notify; } } //finalized unmanged code here m_Disposed = true; } } ~MyClass() {

Proper way to dispose screens in Libgdx

十年热恋 提交于 2019-12-12 07:58:52
问题 What is the proper way of completely disposing of a screen in Libgdx? Currently If I click where a button was on my previous screen the button still does what it would of done if I were on that screen. Should I be .dispose() -ing everything I can in the dispose() method? or is there a simpler way to dispose of everything on screen? 回答1: Unfortunately there is no easier way. These classes do not share any kind of common " Disposable " interface, or anything like that, to do it automatically.

Does one need to close both NetworkStream and TcpClient, or just TcpClient?

╄→尐↘猪︶ㄣ 提交于 2019-12-12 07:46:56
问题 I'm reading the documentation on TcpClient.Close() and noticed this: Calling this method will eventually result in the close of the associated Socket and will also close the associated NetworkStream that is used to send and receive data if one was created. So, correct me if I'm wrong but this says that if one calls Close() on the TcpClient that the NetworkStream will also be closed. So then why at the end of the code example are both Close() called? networkStream.Close(); tcpClient.Close();

How to make sure not to dispose objects that are still used by others

大憨熊 提交于 2019-12-12 05:48:22
问题 I have a class that use constructor injections. public class MyClass { public MyClass(IInterface1 interface1) { } public Dispose() { interface1.dispose(); } } interface1 will be injected by DI. But sometimes, i need to create MyClass manually. public class MyOtherClass { private readonly IInterface1 _interface1; public MyOtherClass() { _interface1 = new Interface1(); } public Foo() { foo = new MyClass(_interface1); bar = new MyClass(_interface1); } } in my dispose method, interface1 is always

How to close a JFrame in the middle of a program

左心房为你撑大大i 提交于 2019-12-12 04:23:55
问题 public class JFrameWithPanel extends JFrame implements ActionListener, ItemListener { int packageIndex; double price; double[] prices = {49.99, 39.99, 34.99, 99.99}; DecimalFormat money = new DecimalFormat("$0.00"); JLabel priceLabel = new JLabel("Total Price: "+price); JButton button = new JButton("Check Price"); JComboBox packageChoice = new JComboBox(); JPanel pane = new JPanel(); TextField text = new TextField(5); JButton accept = new JButton("Accept"); JButton decline = new JButton(

Invoking Something Twice Leads To: “protected override void Dispose”

久未见 提交于 2019-12-12 04:22:05
问题 I have a function that helps me close forms without getting crossthread errors: public void OutsideClose(long Id) { MessageBox.Show(""); if (InvokeRequired) { Invoke(new Action<long>(OutsideClose), Id); } else { var asdf = ListForm.Find(a => a.Id == Id); if (asdf != null) { asdf.Close(); } } } For some reason, if I call this invoke twice, instead of closing the form the second time, it goes to this dispose method: protected override void Dispose(bool disposing) { if (disposing && (components

Should I heed this superficially nonsensical Code Analysis warning? [duplicate]

假如想象 提交于 2019-12-12 03:16:53
问题 This question already has answers here : Why does Code Analysis tell me, “Do not dispose objects multiple times” here: (2 answers) Closed 3 years ago . When I select Analyze > RUn Code Analysis on Solution in Visual Studio 2013, I get, " CA2202 Do not dispose objects multiple times Object 'fs' can be disposed more than once in method 'RoboReporterSQL.SaveReportDataToDB(string, string)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an

Where is the proper place to dispose an image

天大地大妈咪最大 提交于 2019-12-12 03:08:53
问题 I have a form with OpenFileDialog for selecting image and showing it in pictureBox . Until the form is open the user can open and then save the opened image as many times as he wants. What I want to do is, after each new selection-save, to delete the previously saved image if there is such. The problem is that as I implemented the code now I am able to delete the image the first time, if I keep on saving images with the currently open form I get an error that the resource is being used. What