您何时不能在Finalizer / IDisposable上使用SafeHandle? [英] When can't you use SafeHandle over Finalizer/IDisposable?
问题描述
当看到整个finalizer / IDisposable问题时,通常会看到,在进行了很长的描述之后,总会有一些含义,大声笑我所说的实际上是无用的,您应该使用再用SafeHandle再见!
所以我想知道在什么情况下SafeHandle不适合使用,所以您不得不求助于finalizer / IDisposable旧方法?
When seeing about the whole finalizer/IDisposable issue, it is usual to see that, at the end, after all the long description, there will be something to the meaning of "LOL what I said was actually useless, you should use SafeHandle instead bye!" So I am wondering in what case does SafeHandle not fit, such that you have to resort to the finalizer/IDisposable old way?
推荐答案
很显然,当您要包装的非托管资源不是通过句柄获取时。这是罕见的,但并非闻所未闻。一个示例是用C ++ / CLI代码编写包装器,通常是通过包装本地C ++类来完成的。然后资源就是内存。
Clearly, when the unmanaged resource you are wrapping is not acquired through a handle. Which is rare but not unheard of. An example would be writing wrappers in C++/CLI code, commonly done to wrap a native C++ class. The resource is then memory. The unmanaged kind.
不过,您可以花大量时间编写托管代码,而从不编写终结器。终结器属于框架类。
Nevertheless, you can spend a career writing managed code and never write a finalizer. Finalizers belong in the framework classes.
这篇关于您何时不能在Finalizer / IDisposable上使用SafeHandle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!