finalizer

Finalizers and Dispose

I've got a class named BackgroundWorker that has a thread constantly running. To turn this thread off, an instance variable named "stop" to needs to be "true". To make sure the thread is freed when the class is done being used, I've added IDisposable and a finalizer that invokes Dispose(). Assuming that "stop = true" does indeed cause ...

Is the destructor called if the constructor throws an exception?

Looking for an answer for C# and C++. (in C#, replace 'destructor' with 'finalizer') ...

RAII in Ruby (Or, How to Manage Resources in Ruby)

I know it's by design that you can't control what happens when an object is destroyed. I am also aware of defining some class method as a finalizer. However is the ruby idiom for C++'s RAII (Resources are initialized in constructor, closed in destructor)? How do people manage resources used inside objects even when errors or exception...

What is the scope of finalizer thread - per application domain or per process?

Based on all my reading there should be one GC thread to invoke all finalizers. Now, the question is what is the scope of this "one" thread - per process or per application domain, as the whole intention of domains is to separate and make "independent" different applications in one process space. I read here: If an unhandled excepti...

Static Finalizer

What is the right way to perform some static finallization? There is no static destructor. The AppDomain.DomainUnload event is not raised in the default domain. The AppDomain.ProcessExit event shares the total time of the three seconds (default settings) between all event handlers, so it's not really usable. ...

How to identify the GC Finalizer thread?

I have a .NET (C#) multi-threaded application and I want to know if a certain method runs inside the Finalizer thread. I've tried using Thread.CurrentThread.Name but it doesn't work (returns null). Anyone knows how can I query the current thread to discover if it's the Finalizer thread? ...

Python: flush a buffer before program termination via a finalizer

I keep a cache of transactions to flush (to persistent storage) on the event of a watermark or object finalization. Since __del__ is no longer guaranteed to be called on every object, is the appropriate approach to hook a similar function (or __del__ itself) into atexit.register (during initialization)? If I'm not mistaken, this will ca...

Problem disposing of socket / finalising twice?

I'm working with some code (not mine I hasten to add, I don't trust this much at all) for a class which opens a socket, makes requests and listens for responses, which is throwing an exception in a way I can't comprehend when tested in xunit. I assume the same exception happens "live" but the class is referenced by a singleton so it is p...

Need to implement a finalizer on a class that uses TcpClient?

I have a class (say MyClass) that uses (has as a private field) a TcpClient object. MyClass implements IDisposable calling TcpClient.Close in the Dispose method. My question is should MyClass also implement a finalizer to call Dispose(bool Disposing) to free the TcpClient’s unmanaged resources in case MyClass.Dispose is not called by t...

Why isn't my .net destructor called in this very simple scenario?

I've got the following code : public class A { ~A() { Console.WriteLine("destructor"); } } public static A Aref; static void Main(string[] args) { Aref = new A(); int gen = GC.GetGeneration(Aref); Aref = null; GC.Collect(gen, GCCollectionMode.Forced); ...

is it legal to recreate a rooted reference to 'this' in a .net destructor ?

Is it legal to write the following in .net ? public class A { public int i = 0; ~A() { Aref = this; } } public static A Aref; static void Main(string[] args) { Aref = new A(); int gen = GC.GetGeneration(Aref); Aref = null; GC.Collect(gen...

Costs involved with C# destructors (aka: finalizers)?

The destructor should only release unmanaged resources that your object holds on to, and it should not reference other objects. If you have only managed references you do not need to (and should not) implement a destructor. You want this only for handling unmanaged resources. Because there is some cost to having a destructor, you ough...

Should GC.SuppressFinalize be called on objects that do not have a finalizer?

For some reason FXCop seems to think I should be calling GC.SuppressFinalize in Dispose, regardless of whether I have a finalizer or not. Am I missing something? Is there a reason to call GC.SuppressFinalize on objects that have no finalizer defined? ...

.NET - Finalizers and exit(0)

I have a .NET C# / C++ app which uses a call to exit(0) (from <stdlib.h>) in a thread in order to terminate. The strange part is, under some circumstances, the finalizers of the managed objects are called right after the call to exit, and in other circumstances, they are not called at all. The circumstances are pretty deterministic - t...

How important is disposing a Font, really?

I'm aware that the best practice is to call Dispose on any object that implements IDisposable, especially objects that wrap finite resources like file handles, sockets, GDI handles, etc. But I'm running into a case where I have an object that has a Font, and I would have to plumb IDisposable through several layers of objects, and review...

What are finalisers for?

I have been programming in .NET for four years (mostly C#) and I use IDiposable extensively, but I am yet to find a need for a finaliser. What are finalisers for? ...

Dispose & Finalize for collections of properties?

I'm looking at some vb.net code I just inherited, and cannot fathom why the original developer would do this. Basically, each "Domain" class is a collection of properties. And each one implements IDisposable.Dispose, and overrides Finalize(). There is no base class, so each just extents Object. Dispose sets each private var to Nothin...

Convenient way to call GC::KeepAlive in C++/CLI scenarios?

Hi I'm writing some managed wrappers using C++/CLI. The problem is that the GC sometimes disposes the object while I'm using unmanaged members from it. (I think this behaviour is insane but this is another topic). For more details see: http://stackoverflow.com/questions/134653/finalizer-launched-while-its-object-was-still-being-used ht...

Reference to object during finalize

What happens if you save a reference to the current object during the finalize call? For example: class foo { ... public void finalize() { bar.REFERENCE = this; } } Is the object garbage-collected, or not? What happens when you try to access bar.REFERENCE later? ...

Why is NHibernate AdoTransaction's finalizer called?

I'm profiling out unit & integration tests, and I find the a lot of the time is spent on the finalizer of NHibernate.Transaction.AdoTransaction - this means it is not getting disposed properly. I am not using AdoTransaction directly in the code, so it's probably used by some other object inside NHibernate. Any idea what I'm forgetting t...