Topic: CloseHandle for INVALID_HANDLE_VALUE

The documentation says that basically it is not necessary to cause, but sometimes it is necessary. Normally the code looks ~Handle () {if (handle! = NULL && handle! = INVALID_HANDLE_VALUE) CloseHandle (handle);} It turns out, what check on INVALID_HANDLE_VALUE superfluous? Generally, an application should call CloseHandle once for each handle it opens. It is usually not necessary to call CloseHandle if a function that uses a handle fails with ERROR_INVALID_HANDLE, because this error usually indicates that the handle is already invalidated. However, some functions use ERROR_INVALID_HANDLE to indicate that the object itself is no longer valid. For example, a function that attempts to use a handle to a file on a network might fail with ERROR_INVALID_HANDLE if the network connection is severed, because the file object is no longer available. In this case, the application should close the handle.



In the resulted citation there are no instructions on necessity or absence of necessity to cause CloseHandle for descriptors important INVALID_HANDLE_VALUE. Normally at closing it is necessary to check value of a descriptor on equality to value which returns a method for its creation at failure. Besides, for some types of descriptors INVALID_HANDLE_VALUE ((HANDLE)-1) can be correct value, for example for GetCurrentProcess () though to close them it is not necessary. And still it is necessary to check mandatory the value returned CloseHandle ().