FAQ Database Discussion Community


Dispose/finalize pattern : disposing managed ressources

c#,garbage-collection,dispose,finalize
Let's imagine I have a class named Base with 3 attributes : class Base : IDisposable { private string _String; private Class1 classe1; private int foo; public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } public virtual void Dispose(bool disposing) { if (disposing) { Console.WriteLine("Free Managed ressources"); //HOW TO FREE _String, class1...

How to prevent Timer Elapsed event from occurring if forms application closes C#

c#,multithreading,winforms,timer,dispose
I have a timer in a windows forms application (Visual C#) that is causing problems when I want to exit the application. The timer is defined as a member of the form's class: partial class Form1 { //These are the members in question: internal ComACRServerLib.Channel channel; private System.Timers.Timer updateStuff; }...

I cannot close JFrame

java,swing,dispose
I have two java forms: NewJFrame i NewJFrame1. I have button on NewJFrame, so when I click on that button to open NewJFrame1 and close NewJFrame. It can open NewJFrame1, but it cannot close NewJFrame. This: NewJFrame frame = new NewJframe(); frame.setVisible(false); doesn't work. Also, frame.dispose(); doesnt work. CAn someone...

Can you use equals assignment when removing delegate members in a disposing method?

c#,delegates,operators,variable-assignment,dispose
I have the following code in my class public class Receiver : IReceiver { public event EventHandler Received; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) { if (Received != null) { foreach (EventHandler delegateMember in Received.GetInvocationList()) { Received -= delegateMember; } }...

Dispose Stream While Still in Use by an Asynchronous Task

c#,asp.net,asynchronous,stream,dispose
I have a fairly big stream (represents an image). I have two tasks to do: I want to upload the stream to the cloud using an async method (I assume the method is backed by Task.Run) While the stream is being uploaded, I need to locally read the stream (in...

StackOverflowException when disposing an object

c#,asp.net,stack-overflow,dispose
I have created a class to store search values to a session. Such that when I get back to the search page from another page, I can get the values stored in the session. When the search page loads, I'm getting the values from the session, fill up the text...

.NET deterministic disposal of a shared object

.net,c++-cli,dispose
When multiple independent queues may hold references to the same managed object, what is a good method of deterministically disposing of that object when all the queues have finished referencing it? Assume the object in question has both managed and unmanaged resources. Note: Calling the Find() function to test all...

When does the C++/CLI compiler create which dispose pattern?

c#,.net,visual-c++,c++-cli,dispose
This may sound like a weird question but depending on something the compiler generates different content for the dispose methods in the case of a C++/CLI class extending a C# class which implements IDisposable. (If you are interested in what the dispose pattern looks like I recommend reading this or...

C# How to pool the objects of a node tree efficiently?

c#,reference,mono,dispose,pool
I have a node class that contains only value type properties, and one reference type: it's parent node. When performing tree searches, these nodes are created and destroyed hundreds of thousands of times in a very short time span. public class Node { public Node Parent { get; set; }...

Dispose found in existing C# code. Should I get rid of it?

c#,unity3d,kinect,dispose
I found the following code in a Unity3D project using the Kinect v2 which I have taken over. I'm paranoid so I thought I'd check before I delete it. But surely there is no reason for these two lines?? colorFrame.Dispose(); colorFrame = null; This is C#. It has automatic garbage...

Close JFrame pane when button is clicked

java,swing,button,jframe,dispose
I'm trying to use close the pane from an airplane seat system, so each passenger chooses only 1 seat. I researched and know I need a line of code JFrame.dispose(); But I don't know where to put it and what else to put it. Thoughts? (Other than that I'm a...

Java Frames disposal creates extra empty frame

java,swing,jframe,dispose
I am making a simple login application, but every time i press a button to open another frame(From another class) i get an extra empty frame for some reason. I tried testing it by writing a new class and connecting it to another button.. that class just had JFrame.. and...

Disposing of Child Classes inside Lists

vb.net,dispose
I have a container class that represents a set of files. There are 10 different types of files, and 24 hourly files per type of file. I've created a child class per file type, and a list of 24 hourly child classes, so a total of 240 classes. When I...

Static analysis dispose warning in test class when object is disposed in test cleanup

c#,unit-testing,dispose,fxcop
I have a lot of test classes like this. [TestClass] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")] public class TestClass { private IDisposable _disposable; [TestInitialize] public void TestInitialize() { _disposable = //new disposable object...; } [TestCleanup] public void TestCleanup() { _disposable.Dispose(); } [TestMethod] public void Test1() { //Uses _disposable } [TestMethod] public void Test2() {...

c# dispose UserControl Thread

c#,multithreading,user-controls,dispose
I have a MainForm with a UserControl. In can chose which usercontrol I want the show in my MainForm. Everytime I change my usercontrol, I dispose it. private void buttonBar1_ItemClick(object sender, Janus.Windows.ButtonBar.ItemEventArgs e) { foreach (Control control in panelMain.Controls) { control.Dispose(); } panelMain.Controls.Clear(); //... MyUserControl uc = new MyUserControl(); MyUserControl.Dock...

What is the promise disposer pattern?

javascript,promise,dispose,bluebird
I've read about the promise disposer pattern in several places but I can't figure out what it is. It was suggested to me to use it in code that looks like: function getDb(){ return myDbDriver.getConnection(); } var conn = getDb(); var users = getDb().then(function(conn){ return conn.query("SELECT name FROM users").finally(function(users){ conn.release();...

How to handle dispose when it's in a generated partial class

c#,windows-services,dispose
I'm creating a windows service in C# and the dispose method for the Service class is implemented in the generated partial class. Like this: partial class Service { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }...

Best, thread safe way to access a field once i.e. for disposal

c#,multithreading,dispose,interlocked
Basically I want to make sure a field in a class (in this case the _changedPoller) is Disposed as early as possible when not needed anymore. I call StopChangedPolling in the class' Dispose method and when certain events occur. What is the best, thread safe way to dispose a field...

Files not being released, even with .Dispose()

c#,.net,dispose,using
I have a simple C# application for renaming and resizing images, and I have a little problem - when it completes working with all given files, the last one always stays in the memory, or whatever, of the program, and I can't delete it without closing the program. How can...

The ObjectContext instance has been disposed and can no longer be used

c#,asp.net-mvc,entity-framework,dispose
I've seen this question asked a million times, but every single suggestion I've encountered I already seem to have covered. I have entity framework in my MVC solution, and I have a 'repository' which attempts to retrieve a collection of MyObjects: public static List<MyObject> GetMyObjects() { using (var context =...

Base class that does not provide protected Dispose(bool) method?

c#,dispose
MS advices that base class should provide protected virtual void Dispose(bool disposing) in the derived classes. I have an existing class written much earlier which does not provide such function. By knowing the fact base class is Disposable, can we simply use the following in any derived class? class Base...

WPF Control Disposal

c#,wpf,user-controls,dispose,disposal
In WinForms, for all controls there is the .OnDisposed override, the Disposed event, and the IsDisposed property. WPF seems to have no equivalent. How can I listen for the disposal of a UserControl in a WPF application? To be more clear; I need to know when the control is removed....

What's the difference between HttpContext.Current.Session and HttpContext.Current.Items?

asp.net,asp.net-mvc,entity-framework,dispose
I know that the data stored in Session is alive until the user closes the browser while the data stored in Items is alive until the request/response is processed. I also know that if I call Session.Abandon() it will make the Session behave like Items. I made some tests where...

How do I stop a custom Thead in SWT application?

java,multithreading,swt,dispose
How do I stop a custom Thread when I exit my SWT application? MyThread runs in an infinite loop, until a certain condition becomes true: class MyThread extends Thread { @Override public void run() { do { // Do stuff... } while (!this.isInterrupted() && otherCondition); } In my SWT application...