Questions tagged [finalizer]

A finalizer is a special method in an object-oriented language that is executed when an object is garbage collected.

A finalizer is a special method in an object-oriented language that is executed when an object is garbage collected.

Java Documentation (Javadoc) defines the moment when the finalizer is invoked as:

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

However, this may never happen in the life of a program if the object is always accessible. Due to the lack of programmer control over their execution, it is usually recommended to avoid finalizers for any but the most trivial operations.

442 questions
10
votes
3 answers

Why my Close function isn't called?

class Program : CriticalFinalizerObject { static void Main(string[] args) { Program p = new Program(); TextWriterTraceListener listener = new TextWriterTraceListener(@"C:\trace.txt"); …
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
9
votes
1 answer

How can I find the reason for a hung finalizer queue?

I have an application that experiences a slow memory leak from the word go. Using ANTS Memory Profiler I can see that all of the leaked memory is being held by the finalizer queue's GC root. I suspect what may have happened is that the finalizer…
chillitom
  • 24,888
  • 17
  • 83
  • 118
9
votes
5 answers

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?
open-collar
  • 1,404
  • 1
  • 16
  • 22
9
votes
4 answers

Using the Destructor/Dispose of the base class?

In C#, as mentioned in the Documentation, and this nice post's accepted answer, it's stated that classes don't inherit the Destructor of their parent class. The question : If I want to make sure to dispose the private elements of the base class, is…
Tipx
  • 7,367
  • 4
  • 37
  • 59
9
votes
2 answers

How to use PhantomReference as finalize() Replacement

Javadoc 8 for PhantomReference states: Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism. So I tried creating a thread that is calling the…
notes-jj
  • 1,437
  • 1
  • 20
  • 33
9
votes
1 answer

In .NET, can a finalizer be run even if an object's constructor never ran?

I understand that in .NET, finalizers are run even if an object is partially constructed (e.g. if an exception is thrown out of its constructor), but what about when the constructor was never run at all? Background I have some C++/CLI code that does…
rationull
  • 465
  • 4
  • 9
9
votes
1 answer

GC Behavior Inconsistent Between 32-bit and 64-bit Applications

I have noticed inconsistent behavior from the GC when compiling console applications under both 32-bit and 64-bit in .Net 4.0 using VS 2013. Consider the following code: class Test { public static bool finalized = false; ~Test() { …
James Wilkins
  • 6,836
  • 3
  • 48
  • 73
9
votes
5 answers

Is it safe to access a reference type member variable in a finalizer?

In other words, class Foo { object obj; Foo() { obj = new object(); } ~Foo() { obj.ToString(); /* NullReferenceException? */ } }
CannibalSmith
  • 4,742
  • 10
  • 44
  • 52
9
votes
3 answers

Release Excel Object In My Destructor

I'm writing a Excel class using Microsoft.Interropt.Excel DLL. I finish all function but I have an error in my Destructor. I Want to save all changes to my file and I want to release all source. I want to all of them in my destructor. But In my…
Murat
  • 803
  • 4
  • 15
  • 27
9
votes
2 answers

Why event handlers prevent garbage collector from occurring

I have this piece of code public class Publisher { public event EventHandler SomeEvent; } public class Subscriber { public static int Count; public Subscriber(Publisher publisher) { publisher.SomeEvent += new…
Michael
  • 2,961
  • 2
  • 28
  • 54
9
votes
2 answers

Why does the c# garbage collector not keep trying to free memory until a request can be satisfied?

Consider the code below: using System; namespace memoryEater { internal class Program { private static void Main(string[] args) { Console.WriteLine("alloc 1"); var big1 = new BigObject(); …
Sean Reid
  • 911
  • 9
  • 19
9
votes
2 answers

When is it possible to call Finalize in Dispose?

I was browsing the decompiled source code for a DLL in Reflector, and I came across this C# code: protected virtual void Dispose([MarshalAs(UnmanagedType.U1)] bool flag1) { if (flag1) { this.~ClassName(); } else { …
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
8
votes
4 answers

Is it really needed to implement dispose pattern for managed resources only

I've read carefully through this article and it seems to clearly state that the dispose pattern should be implemented in all cases of IDisposable implementation. I'm trying to understand why I need to implement the dispose pattern in cases when my…
8
votes
1 answer

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…
csharptest.net
  • 62,602
  • 11
  • 71
  • 89
8
votes
2 answers

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

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…
Mathieu Garstecki
  • 1,589
  • 1
  • 13
  • 18