1. All functions used to create kernel objects must be very careful when checking their return values. For example:
Handle hmutex = createmutex (...);
If (hmutex = invalid_handle_value)
{
// The code here will not be executed because null is returned when createmutex fails.
}
Handle hfile = createfile (..);
If (hfile = NULL)
{
// The code here will not be executed because invalid_handle_value (-1) is returned when createfile fails)
}
2. No matter how the kernel object is created, we need to call closehandle to show the system that we have ended using the object.
Bool closehandle (handle hobject)
If the handle is valid, the system will obtain the address of the data structure of the kernel object and decrease the "use count" member in the structure. If the technology changes to 0, the kernel object will be destroyed, and removed from the memory.
3. When a process is aborted, the system will ensure that our process does not leave anything and will close the object for us.