Help With Exception
-
First I would like to start thanks to all on the CodeProject I think I am getting a little better at this (this being MFC) Well my code works fine Under the VS 2012 debugger, However When I rebuild it in Release mode. I get a message that my program stopped working with the option of invoking the VS debugger, which I do the code points to MFC code Unhandled Exception going back via the Call Stack it points to CString code Which I will gladly post, Well the Next thing I did seeing the code works fine in Debug mode, was code a CATCH_ALL exception handler just to see if the code with go to the CATCH_ALL block in Which I invoke AfxMessageBox(".... exception handler") and the message was displayed. I think at this point I have to get a little more specific (as to what type of exception) So at the point I think I'll post my code The Bold underline code seems be where the problem is when I go bank via the Call Stack In Debug mode I check the contents and tempstr is a Valid Null Terminated String Can someone at least point me to where I might be able find information exception to catch the specific exception that's giving me the problem Thanks
if(pMsg->message==WM\_KEYDOWN) { TRY { if(pMsg->wParam==VK\_RETURN) { // DebugBreak(); main\_app = (CHERC\_CMDApp \*)AfxGetApp(); memcpy((void \*)main\_app->mybaseeventptr->command,(void \*)"REL",3); memcpy((void \*)main\_app->threadptr\[curr\_thread\]->sockbuffer,(void \*)"REL",3); main\_app->threadptr\[curr\_thread\]->num\_buff = 3; **CString tempstr = PSW.ia**; **tempstr.SetAt(16,0x00); tempstr.MakeUpper();** if(tempstr == end\_address) { memcpy((void \*)main\_app->threadptr\[curr\_thread\]->sockbuffer,(void \*)"TERM",4); main\_app->threadptr\[curr\_thread\]->num\_buff = 4; } ::PostThreadMessage(main\_app->threadptr\[curr\_thread\]->m\_nThreadID,WM\_SEND\_SOCK\_MESS,22,(LPARAM) main\_app->mybaseeventptr->command); ::GetCaretPos(&mycursor); mychar = myedit->CharFromPos(mycursor); longchar = mychar; cursorline = myedit->LineFromChar(longchar); linelen = myedit->LineLength(cursorline); linelen = 125; linechar = myedit->LineIndex(cursorline); myedit->GetLine(cursorline,currline.GetBufferSetLength(linelen +1),linelen); buffptr = currline.GetBuffer(10); myeventptr = main\_app->mybaseeventptr; strncpy((char \*)myeventptr->command,(char \*)&nu\_lls\[0\],5); // single step myeventptr->send\_window = this;
-
First I would like to start thanks to all on the CodeProject I think I am getting a little better at this (this being MFC) Well my code works fine Under the VS 2012 debugger, However When I rebuild it in Release mode. I get a message that my program stopped working with the option of invoking the VS debugger, which I do the code points to MFC code Unhandled Exception going back via the Call Stack it points to CString code Which I will gladly post, Well the Next thing I did seeing the code works fine in Debug mode, was code a CATCH_ALL exception handler just to see if the code with go to the CATCH_ALL block in Which I invoke AfxMessageBox(".... exception handler") and the message was displayed. I think at this point I have to get a little more specific (as to what type of exception) So at the point I think I'll post my code The Bold underline code seems be where the problem is when I go bank via the Call Stack In Debug mode I check the contents and tempstr is a Valid Null Terminated String Can someone at least point me to where I might be able find information exception to catch the specific exception that's giving me the problem Thanks
if(pMsg->message==WM\_KEYDOWN) { TRY { if(pMsg->wParam==VK\_RETURN) { // DebugBreak(); main\_app = (CHERC\_CMDApp \*)AfxGetApp(); memcpy((void \*)main\_app->mybaseeventptr->command,(void \*)"REL",3); memcpy((void \*)main\_app->threadptr\[curr\_thread\]->sockbuffer,(void \*)"REL",3); main\_app->threadptr\[curr\_thread\]->num\_buff = 3; **CString tempstr = PSW.ia**; **tempstr.SetAt(16,0x00); tempstr.MakeUpper();** if(tempstr == end\_address) { memcpy((void \*)main\_app->threadptr\[curr\_thread\]->sockbuffer,(void \*)"TERM",4); main\_app->threadptr\[curr\_thread\]->num\_buff = 4; } ::PostThreadMessage(main\_app->threadptr\[curr\_thread\]->m\_nThreadID,WM\_SEND\_SOCK\_MESS,22,(LPARAM) main\_app->mybaseeventptr->command); ::GetCaretPos(&mycursor); mychar = myedit->CharFromPos(mycursor); longchar = mychar; cursorline = myedit->LineFromChar(longchar); linelen = myedit->LineLength(cursorline); linelen = 125; linechar = myedit->LineIndex(cursorline); myedit->GetLine(cursorline,currline.GetBufferSetLength(linelen +1),linelen); buffptr = currline.GetBuffer(10); myeventptr = main\_app->mybaseeventptr; strncpy((char \*)myeventptr->command,(char \*)&nu\_lls\[0\],5); // single step myeventptr->send\_window = this;
The big difference for variables between DEBUG mode and RELEASE mode is stack variables aren't zeroed. The simple statement
void SomeFunc (void){
int i;}
i is guaranteed to start as zero in DEBUG mode but could be literally anything in RELEASE mode. In the above if you need the variable to start as zero you need it.
void SomeFunc (void){
int i = 0;}
Now you most likely have a structure or pointer variable you are assuming is zero but you have not explicitly set it in a local variable. Your code will then always work in DEBUG mode but almost never works in RELEASE mode.
In vino veritas
-
The big difference for variables between DEBUG mode and RELEASE mode is stack variables aren't zeroed. The simple statement
void SomeFunc (void){
int i;}
i is guaranteed to start as zero in DEBUG mode but could be literally anything in RELEASE mode. In the above if you need the variable to start as zero you need it.
void SomeFunc (void){
int i = 0;}
Now you most likely have a structure or pointer variable you are assuming is zero but you have not explicitly set it in a local variable. Your code will then always work in DEBUG mode but almost never works in RELEASE mode.
In vino veritas
-
First I would like to start thanks to all on the CodeProject I think I am getting a little better at this (this being MFC) Well my code works fine Under the VS 2012 debugger, However When I rebuild it in Release mode. I get a message that my program stopped working with the option of invoking the VS debugger, which I do the code points to MFC code Unhandled Exception going back via the Call Stack it points to CString code Which I will gladly post, Well the Next thing I did seeing the code works fine in Debug mode, was code a CATCH_ALL exception handler just to see if the code with go to the CATCH_ALL block in Which I invoke AfxMessageBox(".... exception handler") and the message was displayed. I think at this point I have to get a little more specific (as to what type of exception) So at the point I think I'll post my code The Bold underline code seems be where the problem is when I go bank via the Call Stack In Debug mode I check the contents and tempstr is a Valid Null Terminated String Can someone at least point me to where I might be able find information exception to catch the specific exception that's giving me the problem Thanks
if(pMsg->message==WM\_KEYDOWN) { TRY { if(pMsg->wParam==VK\_RETURN) { // DebugBreak(); main\_app = (CHERC\_CMDApp \*)AfxGetApp(); memcpy((void \*)main\_app->mybaseeventptr->command,(void \*)"REL",3); memcpy((void \*)main\_app->threadptr\[curr\_thread\]->sockbuffer,(void \*)"REL",3); main\_app->threadptr\[curr\_thread\]->num\_buff = 3; **CString tempstr = PSW.ia**; **tempstr.SetAt(16,0x00); tempstr.MakeUpper();** if(tempstr == end\_address) { memcpy((void \*)main\_app->threadptr\[curr\_thread\]->sockbuffer,(void \*)"TERM",4); main\_app->threadptr\[curr\_thread\]->num\_buff = 4; } ::PostThreadMessage(main\_app->threadptr\[curr\_thread\]->m\_nThreadID,WM\_SEND\_SOCK\_MESS,22,(LPARAM) main\_app->mybaseeventptr->command); ::GetCaretPos(&mycursor); mychar = myedit->CharFromPos(mycursor); longchar = mychar; cursorline = myedit->LineFromChar(longchar); linelen = myedit->LineLength(cursorline); linelen = 125; linechar = myedit->LineIndex(cursorline); myedit->GetLine(cursorline,currline.GetBufferSetLength(linelen +1),linelen); buffptr = currline.GetBuffer(10); myeventptr = main\_app->mybaseeventptr; strncpy((char \*)myeventptr->command,(char \*)&nu\_lls\[0\],5); // single step myeventptr->send\_window = this;
MFC throws exceptions derived from the
CException
class which contain additional information and provide a function to show a message. So you may add this block of code on front of yourCATCH_ALL
block:CATCH(CException, pEx)
{
pEx->ReportError();
pEx->Delete();
}The operation that might fail in your code is
CString::SetAt
(when the position is beyond the end of the string). This will throw an exception in release builds and assert with debug builds. Because you are not getting an assertion with a debug build, the only reason for an exception in release builds can be a different (shorter)PSW.ia
string. If this is not the case the exception source is probably somewhere else. To avoid such out of range acesses you should check the length or use a function that handles them likeCString::Left
. -
How about in release mode I attach the debugger I try to stepping thru the code that's giving me a problem Thanks
Also turn your error level to 4 in release mode and look for any warning along the lines possible use of uninitialized variable.
In vino veritas
-
MFC throws exceptions derived from the
CException
class which contain additional information and provide a function to show a message. So you may add this block of code on front of yourCATCH_ALL
block:CATCH(CException, pEx)
{
pEx->ReportError();
pEx->Delete();
}The operation that might fail in your code is
CString::SetAt
(when the position is beyond the end of the string). This will throw an exception in release builds and assert with debug builds. Because you are not getting an assertion with a debug build, the only reason for an exception in release builds can be a different (shorter)PSW.ia
string. If this is not the case the exception source is probably somewhere else. To avoid such out of range acesses you should check the length or use a function that handles them likeCString::Left
. -
How about pEX->GetErrorMessage..... I think ReportError is just a message box with out any information ?
I must confess that I have not tested if the reporting contains the error message but assumed that it would do so. But you can of course use
GetErrorMessage
with your own reporting. -
MFC throws exceptions derived from the
CException
class which contain additional information and provide a function to show a message. So you may add this block of code on front of yourCATCH_ALL
block:CATCH(CException, pEx)
{
pEx->ReportError();
pEx->Delete();
}The operation that might fail in your code is
CString::SetAt
(when the position is beyond the end of the string). This will throw an exception in release builds and assert with debug builds. Because you are not getting an assertion with a debug build, the only reason for an exception in release builds can be a different (shorter)PSW.ia
string. If this is not the case the exception source is probably somewhere else. To avoid such out of range acesses you should check the length or use a function that handles them likeCString::Left
.