clintMon
asked on
CloseHandle(....) causing crash
My call to CloseHandle(...) causes my program to crash. I tried putting the call inside a try/catch, so that I could display a message box in the event of a problem with this call. However, my program still crashes. The handle argument that I'm passing to CloseHandle(...) seems to be ok. Does anyone know some good exception handling for CloseHandle(....)????
check over your code to make sure the handle has been opened and hasnt been closed elsewhere. The code should look very much like:
HANDLE H;
H = 0;
H = whateverAPIreturnsafile_so cket_pipe_ process_or _graphics_ handle( foo, goo, zoo );
if( H != BADHANDLE ) {
dosoemthingswith( H );
if( H == 0 ) printgf( "OOPS, Handle already closed!!! how did we do this twice?" );
else {
Err = CloseHandle( H );
if( Err != BAD ) printf("oops on handle" );
H = 0;
}
}
HANDLE H;
H = 0;
H = whateverAPIreturnsafile_so
if( H != BADHANDLE ) {
dosoemthingswith( H );
if( H == 0 ) printgf( "OOPS, Handle already closed!!! how did we do this twice?" );
else {
Err = CloseHandle( H );
if( Err != BAD ) printf("oops on handle" );
H = 0;
}
}
You can try using __try / __except to catch the error.
Be aware, that a NULL return value is valid for some API functions, and that not all API functions return NULL or INVALID_HANDLE_VALUE for an error condition.
You need to read the API function documentation to determine what is consider an error (non-initialized) value.
You need to read the API function documentation to determine what is consider an error (non-initialized) value.
um, catching the error is usually regarded as a KLOOODGE.
Sort of like taking aspirin before going down the alley where you're sure to be mugged over the head.
Maybe better to not go down the alley.....
Sort of like taking aspirin before going down the alley where you're sure to be mugged over the head.
Maybe better to not go down the alley.....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I believe that should be the following:
GetHandleInformation(hMyHa ndle, &dwFlags);
GetHandleInformation(hMyHa
According to MSDN:
If the application is running under a debugger, the function will throw an exception if it receives either a handle value that is not valid or a pseudo-handle value. This can happen if you close a handle twice, or if you call CloseHandle on a handle returned by the FindFirstFile function.
More details:
http://msdn2.microsoft.com/en-us/library/ms724211.aspx
Hope this will help you troubleshoot.
Best Regards,
DeepuAbrahamK
If the application is running under a debugger, the function will throw an exception if it receives either a handle value that is not valid or a pseudo-handle value. This can happen if you close a handle twice, or if you call CloseHandle on a handle returned by the FindFirstFile function.
More details:
http://msdn2.microsoft.com/en-us/library/ms724211.aspx
Hope this will help you troubleshoot.
Best Regards,
DeepuAbrahamK
Then when ever you call CloseHandle, set the handle to INVALID_HANDLE_VALUE after making the call.
You can easily make a wrapper function for this that will reset your handle automatically after each call to CloseHandle.