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
4
votes
3 answers

Calling Dispose method will clear and compact the memory in .Net?

I have an idea about Dispose and Finalize method in .Net as laid out below. Is this correct? Dispose : Implement IDisposable interface and remove unused/unmanaged code in the Dispose() method. The developer needs to call it manually if they want…
Syed
  • 931
  • 13
  • 28
4
votes
1 answer

SWIG struct members are freed prematurely by Java's garbage collector

I have a C++ library that is called by Java through a SWIG-based interface. On the Java side, I build up a structure containing pointers to arrays of other structures, using the default struct interface and carrays.i's %array_class. Because Java's…
rpjohnst
  • 1,612
  • 14
  • 21
4
votes
1 answer

Can I reference / use COM objects in my finalizer?

I have a COM type (created using tlbimp.exe) and a C# class that wraps this object. I want to perform some clean up in the finalizer for my C# wrapper. Following the guidelines here I might write something like this: public class MyClass :…
Justin
  • 84,773
  • 49
  • 224
  • 367
4
votes
4 answers

.NET - Finalizers and exit(0)

I have a .NET C# / C++ app which uses a call to exit(0) (from ) 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…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
4
votes
7 answers

Why is it always necessary to implement IDisposable on an object that has an IDisposable member?

From what I can tell, it is an accepted rule that if you have a class A that has a member m that is IDisposable, A should implement IDisposable and it should call m.Dispose() inside of it. I can't find a satisfying reason why this is the case. I…
mphair
  • 1,450
  • 2
  • 9
  • 15
4
votes
1 answer

Calling static methods from C# finalizer

Jeffrey Richter in his CLR via C# book (as seen online in the sample chapter Working with Types Requiring Special Cleanup) indicates the following: Also, the CLR doesn’t make any guarantees as to the order in which Finalize methods are called. So,…
E. Shcherbo
  • 1,128
  • 8
  • 15
4
votes
3 answers

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…
Brann
  • 31,689
  • 32
  • 113
  • 162
4
votes
3 answers

C#: In a finalizer, how to find out if the application is shutting down?

I have a finalizer that seems to always fail during application shutdown. I think this is because it's holding onto some native resources that are no longer valid at that point. Is there a way to tell, in a destructor/finalizer, if it is being…
Robert Fraser
  • 10,649
  • 8
  • 69
  • 93
4
votes
4 answers

Why do C# destructors need to have XML Documentation?

I like to keep the comments and Xml documentation of my C# code fairly minimal. Preferring to make the code self-documenting where possible instead. But the C# compiler gives a warning if I don't put an Xml comment on a destructor of a public…
Andy Lowry
  • 1,767
  • 13
  • 12
4
votes
2 answers

If structs can implement IDisposable, why can't they have destructors?

I read the accepted answer to a similar question, part of the answer is: when structs are passed as parameters, they get passed by value: they are copied. Now you've got two structs with the same internal fields, and they're both going to…
David Klempfner
  • 8,700
  • 20
  • 73
  • 153
4
votes
2 answers

Performance Penalty for Finalizer If Never Called

I have a class with a finalizer. But since I'm always calling Dispose() and Dispose() is calling GC.SupressFinalize(this), I think my object never actually makes it into the finalization queue. The finalizer is just in there as a backstop in case…
Michael Covelli
  • 2,113
  • 4
  • 27
  • 41
4
votes
3 answers

IDisposable with destructor: requires thread-safe implementation?

This is pretty much only for me to make sure, I got this right: We have a large resource class implementing the IDisposal pattern. It should (by design) be implemented in a way, that enables it to get called more than one time (even if we try to…
user492238
  • 4,094
  • 1
  • 20
  • 26
4
votes
5 answers

Correct way of implementing Finalize and Dispose(When parent class implements IDisposable)

I was implementing Finalize and Dispose in my classes, I implemented IDisposable on my parent class and override the Dispose(bool) overload in my child classes. I was not sure whether to use a duplicate isDisposed variable(as its already there in…
akjoshi
  • 15,374
  • 13
  • 103
  • 121
4
votes
2 answers

Finalizers with Dispose() in C#

See the code sample from MSDN: (http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=VS.100).aspx) // Design pattern for a base class. public class Base: IDisposable { private bool disposed = false; //Implement IDisposable. public void…
msuhash
  • 266
  • 3
  • 12
4
votes
2 answers

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 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…
jonathan_ou
  • 756
  • 1
  • 7
  • 17