Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to call CloseHandle (handle) who handle is NULL

Tags:

c++

winapi

mfc

Delete a NULL pointer is secure.

int* p = NULL;
delete p;         // ok, secure

What ist about Handles?

HANDLE h = NULL;
CloseHandle(h);   // allowed?

I am reading MSDN but still not sure. It say something about ERROR_INVALID_HANDLE but it is 6L, not NULL.

I come from a destructor of a class, which gives me a C6387 warning Error

if (m_hThread)
    WaitForSingleObject(m_hThread, INFINITE);
CloseHandle(m_hThread);    // warninig C6387
m_hThread = NULL;
like image 795
Tom Tom Avatar asked Nov 30 '17 14:11

Tom Tom


People also ask

What does CloseHandle do?

In general, CloseHandle invalidates the specified object handle, decrements the object's handle count, and performs object retention checks. After the last handle to an object is closed, the object is removed from the system.

How do you close a process handle?

Assuming we find our handle, what do we do with it? Fortunately, there is a trick we can use that allows closing a handle in another process: call DuplicateHandle again, but add the DUPLICATE_CLOSE_SOURCE to close the source handle. Then close our own copy, and that's it!

How often should I call CloseHandle?

Generally, an application should call CloseHandleonce for each handle it opens. It is usually not necessary to call CloseHandleif a function that uses a handle fails with ERROR_INVALID_HANDLE, because this error usually indicates that the handle is already invalidated.

Is it necessary to call CloseHandle if error_invalid_handle fails?

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.

Can we call CloseHandle on a null handle?

It say something about ERROR_INVALID_HANDLE but it is 6L, not NULL. Show activity on this post. No. You may not call CloseHandle on a NULL handle.

What is the use of CloseHandle in Linux?

In general, CloseHandle invalidates the specified object handle, decrements the object's handle count, and performs object retention checks. After the last handle to an object is closed, the object is removed from the system. For a summary of the creator functions for these objects, see Kernel Objects.


1 Answers

No. You may not call CloseHandle on a NULL handle. Look at the documentation for the argument. It says:

hObject [in] A valid handle to an open object.

NULL is not a handle to an open object.

The fact that you get C6387 is precisely because you are passing a "possibly-null" handle to CloseHandle.

You must write the code as:

if (m_hThread) {
    WaitForSingleObject(m_hThread, INFINITE);
    CloseHandle(m_hThread);
}

(There is no point setting m_hThread to NULL after this - it is going to cease to exist in a very short while).

like image 185
Martin Bonner supports Monica Avatar answered Nov 15 '22 00:11

Martin Bonner supports Monica