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
348 views
in Technique[技术] by (71.8m points)

c# - .NET - Finalizers and exit(0)

I have a .NET C# / C++ app which uses a call to exit(0) (from <stdlib.h>) 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 circumstances, they are not called at all.

The circumstances are pretty deterministic - the app calls some methods from an external plugin dll (written in unmanaged C) during its lifetime.
If I use dll A, the finalizers are always called.
If I use dll B, the finalizers are never called.

What's the expected behaviour of finalizers in case of an exit(0) call? (if there is any expected -and documented- behaviour)

Can the calls to the external dlls change some global setting that may impact the way the process is terminated?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Chris Brumme talked about how finalizers are handled during process shutdown:

The bottom line is that there seems to be very little in the way of guarantees for finalizers running at shutdown, but I'm not sure what the DLLs may be doing to causing things to act differently (maybe it 's that one DLL is doing something in DLL_PROCESS_DETACH processing that's giving .NET the opportunity to process finalizers.

The article is for .NET 1.x - I'm not sure how much of this has changed in .NET 2.0 or later.


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

1.4m articles

1.4m replys

5 comments

57.0k users

...