below is the pseudocode code of SafeHandle
class:
public abstract class SafeHandle : CriticalFinalizerObject, IDisposable { // This is the handle to the native resource protected IntPtr handle; private bool _ownsHandle; protected SafeHandle(IntPtr invalidHandleValue, Boolean ownsHandle) { handle = invalidHandleValue; if (!ownsHandle) GC.SuppressFinalize(this); } ... }
I have two questions on SafeHandle
:
Q1-Why the first parameter is called "invalidHandleValue" rather than "handleValue"? You use SafeHandle when you need to deal with a native resource, why this native resource is "invalid" in the first place?
Q2-Why there is a boolean _ownsHandle field? when you need to deal with a native resource, you assigns this native resource to SafeHandle's handle field handle = invalidHandleValue;
, then this SafeHandle is deemed to have the native resource, so it owns it, how could it possible that it doesn't own the native resource when it has a field that assigned with this native resource?
没有评论:
发表评论