unhandled exception at 0xc0000120
Hello,
My program works fine when I run it in Debug & Use MFC in shared DLL mode.
When I change the configuration mode to Release mode & Use MFC in a Static library - the program gives unhandled exception at 0xc0000120..
It points to
HRESULT CSerial::Close()
{
if(m_hSerialComm != INVALID_HANDLE_VALUE)
{
::CloseHandle(m_hSerialComm);
-->m_hSerialComm = INVALID_HANDLE_VALUE;
}
return S_OK;
}
Please help
[548 byte] By [
chakrijk] at [2007-11-19 18:30:11]

# 1 Re: unhandled exception at 0xc0000120
The problem is likely to be with CloseHandle.
Track the creation and usage of the HANDLE. Always check return values for successful HANDLE creation / closure.
Also, always initialize objects of type HANDLE with NULL.
i.e. Instead of -
HANDLE hSomeHande;
...Always do this -
HANDLE hSomeHande = NULL;
# 2 Re: unhandled exception at 0xc0000120
Except that if you initialize it to NULL, the test
if(m_hSerialComm != INVALID_HANDLE_VALUE)
will be passed, since INVALID_HANDLE_VALUE is -1. CreateFile returns INVALID_HANDLE_VALUE if it fails. When I work with files I initialize the handles with INVALID_HANDLE_VALUE.
cilu at 2007-11-10 23:47:36 >

# 3 Re: unhandled exception at 0xc0000120
Yes - the OP must study the documentation of the API that creates the HANDLE and initialize / test it appropriately.
Not all handle creation API return INVALID_HANDLE_VALUE on failure.
Some return NULL.
# 4 Re: unhandled exception at 0xc0000120
I am initializing HANDLE m_hSerialComm to Null..
CSerial::CSerial()
{
m_hSerialComm = NULL;
}
The creation of the handle - is also no problem as I will get message box if handle is not created..
HRESULT CSerial::Open(constchar *m_pszPortName)
{
HRESULT hResult;
m_hSerialComm = ::CreateFile (m_pszPortName,GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL);
if(m_hSerialComm == INVALID_HANDLE_VALUE)
{
unsignedlong error = ::GetLastError();
hResult = E_FAIL;
AfxMessageBox(_T("Cannot Open Serial Port"), MB_OK | MB_ICONINFORMATION);
}
else
hResult = S_OK;
return hResult;
}
And if I change the function Close() like this,
HRESULT CSerial::Close()
{
::CloseHandle(m_hSerialComm);
/*
if(m_hSerialComm != INVALID_HANDLE_VALUE)
{
::CloseHandle(m_hSerialComm);
m_hSerialComm = INVALID_HANDLE_VALUE;
}
*/
return S_OK;
}
I get message Unhandled exception at 0x00000000
# 5 Re: unhandled exception at 0xc0000120
And if I change the function Close() like this,
I get message Unhandled exception at 0x00000000
Yes, the failure as I said is with CloseHandle.
Okay, looking at the API call that creates a HANDLE, I think you should go the way cilu has recommended. Initialize HANDLE to INVALID_HANDLE_VALUE, and in the call to Close check against it (uncomment the commented lines).
I would not be surprised if you are missing a call to Open on the object you invoke a Close on.
# 6 Re: unhandled exception at 0xc0000120
This code was working fine till yester day - suddenly it started malfunctioning today.. First it was like - I get o/p in Debug mode but not in release mode. Now- totally dumb.
When I initialize HANDLE m_hSerialComm to INVALID_HANDLE_VALUE, it always gives error message - Cannot open Serial Port! Now even Unhandled exception error also wont come. - Only it gives above message.
Why it is not creating the handle? Is there any problem with code?
I even tried by restarting the PC - no difference.
# 7 Re: unhandled exception at 0xc0000120
My program works fine when I run it in Debug & Use MFC in shared DLL mode.
When I change the configuration mode to Release mode & Use MFC in a Static library - the program gives unhandled exception at 0xc0000120.Running your release build in the debugger should help you find out what's wrong. Take a look at this FAQ. (http://www.dev-archive.com/forum/showthread.php?t=269905)
# 8 Re: unhandled exception at 0xc0000120
Now even Unhandled exception error also wont come.
So, this means that the crash is solved, right?
# 9 Re: unhandled exception at 0xc0000120
OK - Thank you!
# 10 Re: unhandled exception at 0xc0000120
Not all handle creation API return INVALID_HANDLE_VALUE on failure.
Some return NULL.
Exactly. For instance CreateEvent() returns NULL if it fails. That's why I emphasized that I initialize handles to INVALID_HANDLE_VALUE when working with files.
cilu at 2007-11-10 23:55:45 >

# 11 Re: unhandled exception at 0xc0000120
When I initialize HANDLE m_hSerialComm to INVALID_HANDLE_VALUE, it always gives error message - Cannot open Serial Port! Now even Unhandled exception error also wont come. - Only it gives above message.
That doesn't have to do with the value you use to initialize the handle. It's most likely that you don't use correct parameters for CreateFile(). Make sure pszPortName is a string like this: "\\\\.\\COM<n>" where <n> is a number. In other words a string like "\\\\.\\COM1", "\\\\.\\COM16". If you try to open COM ports greater than 8 and don't use \\.\ to prefix the name, it will fail. Also CreateFile can fail if the port is already opened.
cilu at 2007-11-10 23:56:48 >

# 12 Re: unhandled exception at 0xc0000120
gstercken,
Thank you for the link.
This helped me..
Turn off optimizations
One thing you can do is turn off all the optimizations in the release version. Go to the Project | Settings for the release version, choose the C/C++ tab, select Optimizations in the combo box, and simply turn off everything. Then do Build | Rebuild All and try again. If the bug went away, then you have a clue. No, you still don't know if it was an optimization bug in the strict sense, but you now know that the bug in your program is a consequence of an optimization transformation, which can be as simple as an uninitialized stack variable whose non-initialized value is sensitive to the optimization of the code. Not a lot of help, but you now know something more than you did before.