質問

I have a static class NativeMethods containing methods decorated with [DllImport]. I want to load DLL dynamically with LoadLibrary in NativeMethods static constructor.

Should I call FreeLibrary explicitly on some (process/AppDomain?) shutdown event? Is letting OS do the cleanup implicitly on process shutdown reserved for the crashes only or a normal case?

役に立ちましたか?

解決

You should do this explicitly. The proper way would be to create a class that holds the library reference, which releases the reference in it's finalizer, just like with any other long living native resource.

It's tempting to just wait for the OS to cleanup (and it will clean it up on process exit, no worries about that), but it can introduce some hard to track issues when you're expanding your software, and it can make many issues (like hanging threads) worse in the long term.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top