Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Why is the finalizer called even though no instance of the class is created? </p> </blockquote> <p>The question makes no sense. Obviously an instance is created; what would the finalizer be finalizing if there wasn't an instance created? Are you trying to tell us that there's no "this" reference in that finalizer? </p> <blockquote> <p>the constructor hasn't been called</p> </blockquote> <p>The constructor can't be called because jitting the constructor references a field whose type is missing. How could a constructor body that can't even be jitted be called?</p> <p>You seem to think that just because a constructor cannot be called, that an instance cannot be created. That doesn't follow logically at all. Clearly there must be an instance <em>before</em> the ctor is called because a reference to that instance is passed to it as "this". So the memory manager creates an instance - and the garbage collector knows that there's memory allocated - and then it calls the constructor. If calling the constructor throws an exception - or is interupted by an asynchronous exception such as a thread abort - there's still an instance there, known to the garbage collector, and therefore in need of finalization when it is dead.</p> <p>Since the object will never be assigned to any live variable -- it cannot be, since the assignment happens after the ctor, and the ctor threw when the jitter tried to jit it -- it will be determined to be dead on the next gen zero collection. It will then be put onto the finalizer queue, which will make it alive.</p> <blockquote> <p>the finalizer is still invoked (on the GC thread), which finally crashes the application.</p> </blockquote> <p>Then fix the finalizer so that it does not do that. </p> <p>Remember, the ctor can be interrupted <em>at any time</em> by an asynchronous exception such as a thread abort. You cannot rely on <em>any</em> invariant of an object being maintained in the finalizer. Finalizers are deeply weird code; you should assume that they can run in arbitrary order on arbitrary threads with the object in an arbitrarily bad state. <strong>You are required to write extremely defensive code inside a finalizer.</strong> </p> <blockquote> <p>If I set the breakpoint in the Foo constructor (after deleting Bar's dll) it is not hit. </p> </blockquote> <p>Correct. As I said, the constructor body cannot even be jitted. How could you hit a breakpoint in a method that cannot even be jitted?</p> <blockquote> <p>This means if I would set have a statement in the constructor that creates a critical resource (before newing up Bar) it wouldn't be executed, hence no need for the finalizer to be called.</p> </blockquote> <p>Whether or not <em>you</em> think a finalizer needs to be called is <em>completely irrelevant</em> to the garbage collector. The finalizer might have other semantics than merely cleaning up resources. The garbage collector does not attempt to psychically determine the developer's intentions and make decisions about whether it <em>needs</em> to call the finalizer or not. <strong>The object was allocated and has a finalizer and you did not suppress finalization on it, so it's gonna be finalized.</strong> If you don't like that then <em>don't make a finalizer.</em> You made a finalizer because presumably you wanted all instances of the object to be finalized, and so they're going to be.</p> <p>Frankly, I would revisit your basic scenario. The idea that you can safely recover and continue to execute code in an appdomain where required DLLs are missing seems like an extremely bad idea to me. Getting this right is going to be very difficult. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload