Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

.net - Exceptions during Finalize(): what methodology are you using to detect garbage collector-time exceptions?

I am developing a pretty extensive system in .NET, which involves a lot of system programming. Most time, I'm using the IDisposable pattern to handle resource disposal, but sometimes this isn't applicable (or left out by mistake) and resource gets destroyed during Finalize(). This might happen in COM interop, or when destructor calls Dispose() and there is an exception inside of it.

Basically: it is not always possible to clearly see and handle each scenario when finalizer might throw. And when it happens, the application will most certainly crash.

The reason why I'm concerned with specifically this class of problems is that finalizers are not called by a thread that created or used an object, so it is almost impossible task to tie the exception to the context in which the object was created. All you are getting is some generic GC thread.

So, question to the public now: if you account for those kind of issues, what do you do to control them? Tag the objects? Use a 3rd party tool which allows tracking those issues?

Also: is it possible to trigger some sort of global "Finalizer threw" event, to at least log that this very problem has happened?

EDIT1: Many thanks to everyone who submitted valuable input, I think I'm somewhat clearer now on what needs to be done. Last thing I would really like to get from this discussion is if somebody knows the methodology to trigger code on exception in finalizer (even if app crash is still inevitable), so that I could at least log it, without having to modify destructor of every class.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your question is based on a somewhat faulty premise, that is it's not possible to to handle each scenario where a Finalizer might throw. This is precisely what you need to achieve here. Exceptions which occur during finalization will kill the process. Unless the exception is truly fatal, in which case let the program crash, you should be handling this exception in a manner that will not crash your program.

Having a Finalizer throw is almost as bad as having C++ destructors throw. Most implementations of IDisposable call into the same method for both active (IDisposable.Dispose()) and passive (finalizer thread) dispose operations. If the Finalizer version is throwing then it's likely or possible that the active dispose could throw as well. This, much like a C++ destructor throwing, will prevent nested resources from being disposed properly in certain cases.

Don't allow exceptions to propagate from a finalizer unless they are truly fatal to your application.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...