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
23
votes
5 answers

Proper cleanup of WPF user controls

I am relatively new to WPF, and some things with it are quite foreign to me. For one, unlike Windows Forms, the WPF control hierarchy does not support IDisposable. In Windows Forms, if a user control used any managed resources, it was very easy to…
jrista
  • 32,447
  • 15
  • 90
  • 130
23
votes
4 answers

Uncaught exception thrown by finalizer: Google API bug Or Samsung kernel bug?

I keep getting this error when launch my app on my galaxy Tab 2 (Samsung). The app i'm developing is quite complicated and it is very hard to track down where this error originates from. So I started to strip down piece by piece my app and I ended…
WiZarD
  • 361
  • 2
  • 5
22
votes
5 answers

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…
Nathaniel Flath
  • 15,477
  • 19
  • 69
  • 94
21
votes
4 answers

How do I unit test a finalizer?

I have the following class which is a decorator for an IDisposable object (I have omitted the stuff it adds) which itself implements IDisposable using a common pattern: public class DisposableDecorator : IDisposable { private readonly…
GraemeF
  • 11,327
  • 5
  • 52
  • 76
19
votes
2 answers

Are .net finalizers always executed?

Are finalizers guaranteed to be executed in .NET at some point (spare power outages and the like)? I know how GC works and that it is nondeterministic when exactly they'll run. (The search did not display good answers, so I'm adding this question in…
mafu
  • 31,798
  • 42
  • 154
  • 247
18
votes
2 answers

Is it safe to call an RCW from a finalizer?

I have a managed object that calls a COM server to allocate some memory. The managed object must call the COM server again to free that memory before the managed object goes away to avoid a memory leak. This object implements IDisposable to help…
Andrew Arnott
  • 80,040
  • 26
  • 132
  • 171
17
votes
6 answers

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…
Joe White
  • 94,807
  • 60
  • 220
  • 330
17
votes
3 answers

F# Equivalent of Destructor

I am translating a C# class that wraps an unmanaged library to F#. I have run into the seemingly simple problem of rewriting the destructor that follows. class Wrapper { // P/Invoke ellided private SomeType x; public Wrapper() { …
Falon
  • 173
  • 1
  • 4
17
votes
6 answers

Good uses of the finalize() method

This is mostly out of curiosity. I was wandering if anyone has encountered any good usage for Object.finalize() except for debugging/logging/profiling purposes ? If you haven't encountered any what would you say a good usage would be ?
Simeon
  • 7,582
  • 15
  • 64
  • 101
16
votes
5 answers

Troubleshooting a java memory leak: finalization?

I have a misbehaving application that seems to leak. After a brief profiler investigation, most memory (80%) is held by java.lang.ref.Finalizer instances. I suspect that finalizers fail to run. A common cause of this seems to be exceptions thrown…
Rom1
  • 3,167
  • 2
  • 22
  • 39
16
votes
1 answer

How does a "finalizer guardian" work in java?

How does a "finalizer guardian" [Effective Java , page 30] work ? Have you used them? Did it solve any specific problem ?
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
16
votes
3 answers

Garbage Collection and Finalizers: Finer Points

In answering another question* on SO, and the subsequent comment discussion, I ran into a wall on a point that I'm not clear on. Correct me on any point where I'm astray... When the Garbage Collector collects an object, it calls that object's…
James King
  • 6,233
  • 5
  • 42
  • 63
16
votes
3 answers

Two questions about Dispose() and destructors in C#

I have a question about how to use Dispose() and destructors. Reading some articles and the MSDN documentation, this seems to be the recommended way of implementing Dispose() and destructors. But I have two questions about this implementation, that…
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
16
votes
2 answers

Good samples of using Finalizers in C#

When I read a few articles about memory management in C#, I was confused by Finalizer methods. There are so many complicated rules which related with them. For instance, nobody knows when the finalizers will be called, they called even if code in…
mt_serg
  • 7,487
  • 4
  • 29
  • 45
16
votes
2 answers

c# finalizer throwing exception?

Quote from MSDN: If Finalize or an override of Finalize throws an exception, the runtime ignores the exception, terminates that Finalize method, and continues the finalization process. Yet if I have: ~Person() { throw new Exception("meh"); } then…
sjhuk
  • 313
  • 2
  • 7
1
2
3
29 30