dispose

Why call dispose(false) in the destructor?

What follows is a typical dispose pattern example: public bool IsDisposed { get; private set; } #region IDisposable Members public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!IsDisposed) { if (disposing) { //perform...

How does the IDisposable interface work?

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? ...

Does my code properly clean up its List<MemoryStream>?

I've got a third-party component that does PDF file manipulation. Whenever I need to perform operations I retrieve the PDF documents from a document store (database, SharePoint, filesystem, etc.). To make things a little consistent I pass the PDF documents around as a byte[]. This 3rd party component expects a MemoryStream[] (MemorySt...

How do you prevent IDisposable from spreading to all your classes?

Start with these simple classes... Let's say I have a simple set of classes like this: class Bus { Driver busDriver = new Driver(); } class Driver { Shoe[] shoes = { new Shoe(), new Shoe() }; } class Shoe { Shoelace lace = new Shoelace(); } class Shoelace { bool tied = false; } A Bus has a Driver, the Driver has t...

returning in the middle of a using block.

Something like: using (IDisposable disposable = GetSomeDisposable()) { //..... //...... return Stg(); } I believe it is not a proper place for a return statement, is it? ...

Dispose on user controls, really meant to edit the .designer.cs file?

For a user control with internal data structures that must be disposed, is the correct place to add that code to the Dispose method in the .designer.cs file, or is there an event or something we're meant to use instead? Edit: This is a winforms user control. ...

Who Disposes of an IDisposable public property?

If I have a SomeDisposableObject class which implements IDisposable: class SomeDisposableObject : IDisposable { public void Dispose() { // Do some important disposal work. } } And I have another class called AContainer, which has an instance of SomeDisposableObject as a public property: class AContainer { Som...

How to force SQLconnection to release a database???

I have written a small application, that can restore a database (C# and SQL2005), but after I have accessed the database, I can't drop it - it says that it is in use.. I guess it has to do with the SQLconnection-pooling, but can I force it to relase the database?? ...

C# cleanup unmanaged resources when process is killed

I have a class that instantiates a COM exe out of process. The class is public class MyComObject:IDisposable { private bool disposed = false; MyMath test; public MyComObject() { test = new MyMath(); } ~MyComObject() { Dispose(false); } public double GetRandomID() { if (...

How do I close an OracleConnection in .NET

Say I have these two objects: OracleConnection connection = new OracleConnection(connectionString); OracleCommand command = new OracleCommand(sql, connection); To close the connection or Oracle, do I have to call command.Dispose(), connection.Dispose(), or both? Is this good enough: using(connection) { OracleDataReader reade...

Is it safe to believe Page_Unload will always run and is a good place for Dispose() code?

I'm familiar with the try{}finally{} pattern, the using(){} pattern as ways to ensure Dispose() gets called, but for an ASP.NET page, is it just as safe to Dispose of objects created in page scope on the Page_Unload event? Would it make sense to override the Page's Dispose() method instead? I'm not sure what code raises the Page_Unload...

Disposing a form from parent form in C#?

I have a form which will open a new form when one button (form1button) is clicked. And on the child form there will be another button 'form2button'. Now if I click this form2button the new form2 should be disposed. But because the form2 object is created here in form1 class method, I cannot dispose that object in form2 class method (fom2...

Finalize vs Dispose

Why do some people use the Finalize method over the Dispose method? In what situations would you use the Finalize method over the Dispose method and vice versa? ...

How to handle starting/stopping API interface that may take a long time to start

I have an application that needs to work with a vendor-supplied API to do Screen Pops when a call is routed to the user's extension. Another developer worked on getting the API to work and delivered a prototype wrapper class that isn't quite right, and I'm trying to get it right. Because this API can block for several minutes before re...

How can I dispose System.Xml.XmlWriter in PowerShell

I am trying to dispose XmlWriter object: try { [System.Xml.XmlWriter] $writer = [System.Xml.XmlWriter]::Create('c:\some.xml') } finally { $writer.Dispose() } Error: Method invocation failed because [System.Xml.XmlWellFormedWriter] doesn't contain a method named 'Dispose'. On the other side: $writer -is [IDisposab...

Passing an IDisposable object by reference causes an error?

I am trying to create a general method for disposing an object that implements IDisposable, called DisposeObject() To make sure I am disposing an object pointed by original reference, I am trying to pass an object by reference. But I am getting a compilation error that says The 'ref' argument type doesn't match parameter type In ...

Is this IDisposable implementation correct?

I can never remember all the rules for implementing the IDisposable interface, so I tried to come up with a base class that takes care of all of this and makes IDisposable easy to implement. I just wanted to hear your opinion if this implementation is ok as it is or whether you see something I could improve. The user of this base class i...

Should I Dispose() DataSet and DataTable?

DataSet and DataTable both implement IDisposable, so, by conventional best practices, I should call their Dispose() methods. However, from what I've read so far, DataSet and DataTable don't actually have any unmanaged resources, so Dispose() doesn't actually do much. Plus, I can't just use using(DataSet myDataSet...) because DataSet ha...

How does one release/dispose/destroy captured variables in anonymous methods?

I am using anonymous methods to handle events in a COM object. Once the program terminates, it appears that the resources I am using in the anonymous method are not being "closed correctly" in that I get a first chance exception (InvalidComObjectException) for every resource I was watching. I suppose this isn't a big deal, but it doesn't...

WinForm disposing a sub form when the form is disposed

I have a form that can open a sub form (with ShowDialog). I want to make sure that the sub form is being disposed properly when the main form is done. I tried adding the subform to the components member of the main form, but at the moment I got a ArgumentNullException. I know I can just instantiate the components myself, but isn't that a...