Destructors cannot be called

I came across the following quote: "Desctructors are not guaranteed to be called." and it scares me a little.

It is further stated that even a finally try block can be interrupted, resulting in a memory leak. This provides a solution, either by placing your code in a CER (restricted execution area), or obtained from a CriticalFinalizerObject .

My question

  • What are the traits using CriticalFinalizerObject , if any?
  • Were they found anyway from CriticalFinalizerObject , which was really useful?
  • Should I worry about using this when starting a memory leak?
+8
c #
source share
4 answers

Why do you rely on descriptors? in most cases you do not need them.

Perhaps see IDisposeable and Dispose Pattern.

Here are some links that help me understand this topic.

-> Everyone thinks about garbage collection in the wrong way

-> How to implement a placement template

-> Implementing Finalize and Dispose to clean up unmanaged resources

+3
source share

Regarding question number 3: memory leaks were usually caused by:

  • Unmanaged resources are not freed. For them, using IDisposable (with the Dispose () callback in the finalizer) is the best approach.

  • Links to managed objects that are supported because other objects still point to them, even if they should be deleted. IHMO, this is more of a code quality problem than a low level garbage collection problem.

If you do not run into memory leaks, you should not even worry about it, and not try to force any behavior.

+1
source share

I would suggest using the IDisposable interface for all resources that need to be destroyed, and use them in the using block.

0
source share

Typically, the differences between regular finalizers and critical finalizers become important when unloading AppDomain. Since most unmanaged resources automatically disappear when the process is unloaded, you usually only need to worry about critical finalization if you want to carefully unload AppDomain while saving the process.

0
source share

All Articles