Finalizer stuck in infinite loop
Asked Answered
N

2

6

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)
        {
        }
    } 

    ~SomeClass()
    {
        Dispose();
    }
}  

1) Does the object get finalized when no longer referenced after the next GC? My answer was NO, because the finalization thread will be stuck on the infinite loop.

2) What could be done in the Dispose to end the finalization and how many times will the loop continue before the object is Disposed ( with out taking in to account the time it will spend in the next Gen )

I am not particularly clear of the exact question (2) .I kinda ran out of time ...

Not knowing the answer I put a static counter that gets to 3 and calls break and stated 3 which technically would work :), but that's not the answer

I'm guessing it as something to do with GC.SupressFinalize() ? maybe calling GC.SupressFinalize() before entering the loop ?

any ideas if not on the answer to the unclear question , more as to what they might of been aiming for ?

Negrophobe answered 11/1, 2012 at 20:17 Comment(1)
By any means this is a curious interview question - while in depth knowledge about the GC is a valid area to poke in in general, I don't know what any answer to this particular question will show the interviewer (besides the fact that you know there is a single thread that performs finalization calls)Extraction
B
8

It's rather immaterial what happens. The CLR will terminate the program, there is a 2 second timeout on a finalizer.

Broadcaster answered 11/1, 2012 at 20:25 Comment(8)
@Oded - Richter's CLR via C#, page 478 according to this blog post: nitoprograms.blogspot.com/2009/08/…Broadcaster
I can't repro this finalizer timeout in a console app, seems to happily run - the link above only seems to refer to finalizers run at process exit timeExtraction
just to refer back to the question what could be done in the dispose() so that the object is disposed ?Negrophobe
of course ... but i don't think that's what they meant since they asked how many more loops will it complete before Dispose endsNegrophobe
@HansPassant: Can you provide a simple repro sample for this (undocumented) timeout behavior? I am not able to.Extraction
@Broken - it's easy, just create an instance of this class in the Main() method. And note that it takes 2 seconds for the process to exit.Broadcaster
@HansPassant: That only applies if the process is exiting though, not in general. Creating an instance of the class in a separate method, calling that method from the main thread, forcing a GC collection and waiting afterwards will show that the finalizer never exits (evidenced i.e. by Console.WriteLine's)Extraction
@Broken - a real program is going to exit. The OOM exception will help :)Broadcaster
I
0

you can check for the disposed state of the object using an boolean variable which will help the dispose method from going into an infinite loop

class SomeClass : IDisposable
{

    bool _disposed = false;

    public void Dispose()
    {
        while (true && !_disposed)
        {
            _disposed = true;
            Console.WriteLine("disposed");
        }
    }

    ~SomeClass()
    {
        Dispose();
    }
}
Invigorate answered 7/3, 2013 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.