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
7
votes
1 answer

How do I write proper destructors and finalizers?

I am trying to figure out how to properly clean up after my objects in C++/CLI. I have read or skimmed these two articles (one, two) and looked at the standard and looked at some other questions notably this one. I have various pieces of…
Sarien
  • 6,647
  • 6
  • 35
  • 55
7
votes
5 answers

When would dispose method not get called?

I was reading this article the other day and was wondering why there was a Finalizer along with the Dispose method. I read here on SO as to why you might want to add Dispose to the Finalizer. My curiousity is, when would the Finalizer be called…
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
7
votes
2 answers

How to properly implement a finalizer for detecting resource leaks in Java

Let's say I have created some resource class with a close() method for cleaning up the resource, and I want to override finalize() to free the resource (and print a warning) if someone has forgotten to call close(). How can this be done…
Soulman
  • 2,910
  • 24
  • 21
7
votes
4 answers

(.net) CriticalFinalizerObject - What does it really do?

My understanding about this class is that you should use it when you want to be sure that the Finalizer (destructor) of the class is called, but from a couple of tests I did, it doesn't seem to be true. If it does not make sure that the dispose…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
7
votes
2 answers

Finalizer not called after unhandled exception even with CriticalFinalizerObject

I have test code like this: public class A : CriticalFinalizerObject { ~A() { File.WriteAllText("c:\\1.txt", "1z1z1"); } } class Program { static void Main(string[] args) { A a = new A(); throw new…
IlyaP
  • 378
  • 3
  • 8
6
votes
2 answers

Resurrection difference in using Object Initializer

I have this code: Essentially i'm trying to demonstrate the use of the c# finalizer and make an object that cannot die, I called it Zombie. Now, normally this demo works great, but today I tried using the same code with the object initializer…
jeroentrappers
  • 130
  • 1
  • 8
6
votes
2 answers

Finalizer stuck in infinite loop

I came across a interview question which i did not know the answer ( little help :) ) well it stated something of the sort : Class SomeClass : IDisposable { public void Dispose() { while(true) { } } …
eran otzap
  • 12,293
  • 20
  • 84
  • 139
6
votes
3 answers

When can't you use SafeHandle over Finalizer/IDisposable?

When seeing about the whole finalizer/IDisposable issue, it is usual to see that, at the end, after all the long description, there will be something to the meaning of "LOL what I said was actually useless, you should use SafeHandle instead bye!" So…
ill mg
  • 331
  • 4
  • 8
6
votes
4 answers

Can anyone explain this finalisation behaviour

Whilst 'investigating' finalisation (read: trying stupid things) I stumbled across some unexpected behaviour (to me at least). I would have expected the Finalise method to not be called, whereas it gets called twice class Program { static void…
Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
6
votes
1 answer

Proper finalization in Python

I have a bunch of instances, each having a unique tempfile for its use (save data from memory to disk and retrieve them later). I want to be sure that at the end of the day, all these files are removed. However, I want to leave a room for a…
6
votes
4 answers

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…
CSharpeProgrammer
6
votes
1 answer

What causes the InvalidComObjectException: "COM object that has been separated from its underlying RCW cannot be used."?

I've looked at the various questions that mention this specific exception (this question lists many of them, which I've visited). Also, I have the same general question as this post, but in a different context, so the answer is not helpful to…
bentsai
  • 3,063
  • 1
  • 27
  • 29
6
votes
1 answer

Calling .Dispose() on a class that has a Finalizer

According to Essential C# 6.0 you should: AVOID calling Dispose() on owned objects that have a finalizer. Instead, rely on the finalization queue to clean up the instance. Could someone please elaborate on this as I'm not clear on what the…
Storm
  • 1,848
  • 4
  • 20
  • 39
6
votes
1 answer

Java GC Question: How could an object become unreachable while one of its methods is still being executed?

I have been reading these slides about Java finalizers. In it, the author describes a scenario (on slide 33) whereby CleanResource.finalize() could be run by the finalizer thread while CleanResource.doSomething() is still running on another thread.…
Neil Traft
  • 18,367
  • 15
  • 63
  • 70
6
votes
2 answers

Why is it not a good idea to serialize an object in its finalizer?

In the book Headfirst C#, I get the point of view that "it's not a good idea to serialize an object in its finalizer, since serialization requires the whole object tree to be in the heap, but you may end up with missing vital part of your program…
Hohenheim
  • 175
  • 2
  • 6