finalizer

Will SqlConnection get disposed by GC?

Disclaimer: I know IDisposable should be implemented when dealing with unmanaged resources. The rest of the code should be deterministic and do using (...) { } (equivalent of try {} finally { Dispose(); }) to guarantee a cleanup as soon as possible. Also, the GC will not call Dispose(), so the recommended pattern is to override the Fin...

InvalidComObjectException when using Excel Interop

I get an InvalidComObjectException after I close my application in the following piece of code: class MyExcelManager { myExelAppInstance = new Excel.Application(); // finalizer ~MyExcelManager() { myExelAppInstance.Quit(); // InvalidComObjectException thrown here myExelAppInstance = null; } } Why is that? Shouldn't ...

Use Dispose() or finalizer to clean up managed threads?

Suppose I have a message pump class in C++0x like the following (note, SynchronizedQueue is a queue of function<void()> and when you call receive() on the queue and it is empty, it blocks the calling thread until there is an item to return): class MessagePump { private: bool done_; Thread* thread_; SynchronizedQueue queue_;...

Removing COM event handlers automatically in C#

I have a standalone .exe COM server and a trivial C# test program that launches an instance of the server and closes it immediately afterwards. Works like a charm. However, if I close the C# program itself I get an exception during the finalizer run because it is trying to unregister/unadvise COM event sinks from the no longer running CO...

What if a finalizer makes an object reachable?

In Java, finalize is called on an object (that overrides it) when it's about to be garbage collectioned, so when it's unreachable. But what if the finalizer makes the object reachable again, what happens then? ...

Finalizer and IDisposable

Based on the documentation (MSDN: link), it is clear that one should use the IDisposable pattern when implementing a finalizer. But do you need to implement a finalizer if you implement IDisposable (so as to provide a deterministic way of disposing the object), and you dont have any unmanaged resources to clean up? As I see it, if the ...

Can I prevent an uncaught exception in another AppDomain from shutting down the application ?

Hello, I'm having trouble with a misbehaved library that throws an exception in a finalizer, which of course crashes the application. To avoid this, I tried loading the library in its own AppDomain, but the exception still bubbles to the surface and crashes the application. As documented on MSDN, registering to AppDomain.UnhandledExce...

AppDomain.Unload throws in Finalizer?

So here is the story so far, I have this worker thingy which uses an AppDomain to perform some task. The domain is expensive to setup and teardown. So I create a cache per-thread of WeakReference objects to the worker thingy like so: class Worker { [ThreadStatic] static Dictionary<string, WeakReference> _workers; public s...