Release build problem
-
I am having a strange problem with the following dialog callback function:
BOOL CALLBACK MergeProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
static int MostRooms;
bool bError;
char *room,*year,*exam;switch(Message) { case WM\_INITDIALOG: if (strlen(dlgdata\[0\])) { year = new char \[10\]; strncpy(year,dlgdata\[0\]+2,4); strcpy(year+4,"\\0"); SendDlgItemMessage(hwnd,SD\_MERGE\_YEAR, WM\_SETTEXT,20,(LPARAM) year); delete year; } SendDlgItemMessage(hwnd,SD\_MERGE\_YEAR, EM\_LIMITTEXT,4,0); SendDlgItemMessage(hwnd,SD\_MERGE\_ROOM, EM\_LIMITTEXT,30,0); SendDlgItemMessage(hwnd,SD\_MERGE\_EXAM, EM\_LIMITTEXT,10,0); MostRooms = atoi(dlgdata\[1\]); EnumChildWindows(hwnd,EnumChildProc,(LPARAM)hFnt); return TRUE; case WM\_COMMAND: switch (LOWORD(wParam)) { case IDOK: bError = false; room = new char \[30\]; year = new char \[10\]; exam = new char \[30\]; room\[0\] = (char)10; year\[0\] = (char)10; exam\[0\] = (char)30; SendDlgItemMessage(hwnd,SD\_MERGE\_YEAR,EM\_GETLINE,0,(LPARAM)year); SendDlgItemMessage(hwnd,SD\_MERGE\_ROOM,EM\_GETLINE,0,(LPARAM)room); SendDlgItemMessage(hwnd,SD\_MERGE\_EXAM,EM\_GETLINE,0,(LPARAM)exam); if (strlen(room) > 0) { if (!count\_rooms(room,MostRooms+1) ) { char tmp \[128\]; sprintf(tmp,"Please enter rooms between 0 and %i!",MostRooms+1); MessageBox(0,tmp,0,0); bError = true; } } else if (!bError) { MessageBox(hwnd,"Please enter a room!",0,0); SetFocus(GetDlgItem(hwnd,SD\_MERGE\_ROOM)); bError = true; } if ( (strlen(year) > 0) && (!bError) ) { if ((atoi(year) > 2000)&&(atoi(year) < 2100)) { char \*text = new char \[10\]; strcpy(text,"¸ß"); strcat(text,year);
-
I am having a strange problem with the following dialog callback function:
BOOL CALLBACK MergeProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
static int MostRooms;
bool bError;
char *room,*year,*exam;switch(Message) { case WM\_INITDIALOG: if (strlen(dlgdata\[0\])) { year = new char \[10\]; strncpy(year,dlgdata\[0\]+2,4); strcpy(year+4,"\\0"); SendDlgItemMessage(hwnd,SD\_MERGE\_YEAR, WM\_SETTEXT,20,(LPARAM) year); delete year; } SendDlgItemMessage(hwnd,SD\_MERGE\_YEAR, EM\_LIMITTEXT,4,0); SendDlgItemMessage(hwnd,SD\_MERGE\_ROOM, EM\_LIMITTEXT,30,0); SendDlgItemMessage(hwnd,SD\_MERGE\_EXAM, EM\_LIMITTEXT,10,0); MostRooms = atoi(dlgdata\[1\]); EnumChildWindows(hwnd,EnumChildProc,(LPARAM)hFnt); return TRUE; case WM\_COMMAND: switch (LOWORD(wParam)) { case IDOK: bError = false; room = new char \[30\]; year = new char \[10\]; exam = new char \[30\]; room\[0\] = (char)10; year\[0\] = (char)10; exam\[0\] = (char)30; SendDlgItemMessage(hwnd,SD\_MERGE\_YEAR,EM\_GETLINE,0,(LPARAM)year); SendDlgItemMessage(hwnd,SD\_MERGE\_ROOM,EM\_GETLINE,0,(LPARAM)room); SendDlgItemMessage(hwnd,SD\_MERGE\_EXAM,EM\_GETLINE,0,(LPARAM)exam); if (strlen(room) > 0) { if (!count\_rooms(room,MostRooms+1) ) { char tmp \[128\]; sprintf(tmp,"Please enter rooms between 0 and %i!",MostRooms+1); MessageBox(0,tmp,0,0); bError = true; } } else if (!bError) { MessageBox(hwnd,"Please enter a room!",0,0); SetFocus(GetDlgItem(hwnd,SD\_MERGE\_ROOM)); bError = true; } if ( (strlen(year) > 0) && (!bError) ) { if ((atoi(year) > 2000)&&(atoi(year) < 2100)) { char \*text = new char \[10\]; strcpy(text,"¸ß"); strcat(text,year);
waldermort wrote:
only with a release build,
Debug builds set local variables to zero (false) - release builds don't (so what you have is basically indeterminate). It's important to always initialize your local variables to a known state. I'd start by setting
bError
tofalse
when you declare it and check if the problem reccurs. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com -
waldermort wrote:
only with a release build,
Debug builds set local variables to zero (false) - release builds don't (so what you have is basically indeterminate). It's important to always initialize your local variables to a known state. I'd start by setting
bError
tofalse
when you declare it and check if the problem reccurs. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)comI have already tried this. When declaring the berror variable I origionaly set it to false. Though thinking this may be a cause of the problem I removed it and instead set it to false in the IDOK handler. Like I said in my post, bError is false right up until the final test, where for some reason it is being set to true. The problem lies here somewhere
if ((strlen(exam) > 0) && (!bError) ) {
}
else if (!bError) {
MessageBox(hwnd,"Please enter an exam name!",0,0);
SetFocus(GetDlgItem(hwnd,SD_MERGE_EXAM));
bError = true;
}However the "Please enter an exam name!" is never shown, suggesting the
bError = true;
line is never executed either. -
I have already tried this. When declaring the berror variable I origionaly set it to false. Though thinking this may be a cause of the problem I removed it and instead set it to false in the IDOK handler. Like I said in my post, bError is false right up until the final test, where for some reason it is being set to true. The problem lies here somewhere
if ((strlen(exam) > 0) && (!bError) ) {
}
else if (!bError) {
MessageBox(hwnd,"Please enter an exam name!",0,0);
SetFocus(GetDlgItem(hwnd,SD_MERGE_EXAM));
bError = true;
}However the "Please enter an exam name!" is never shown, suggesting the
bError = true;
line is never executed either.Sometimes it helps to rewrite the logic tests to be more affirmative. In the example you show, you're making use of what I call "testing for a negative" which can be very hard to follow. Try rewriting the tests like this:
if ( (strlen(exam) <= 0) && ( bError == false ) )
{
MessageBox(hwnd,"Please enter an exam name!",0,0);
SetFocus(GetDlgItem(hwnd,SD_MERGE_EXAM));
bError = true;}
This way is simpler, clearer, and easier to follow mentally.
-
Sometimes it helps to rewrite the logic tests to be more affirmative. In the example you show, you're making use of what I call "testing for a negative" which can be very hard to follow. Try rewriting the tests like this:
if ( (strlen(exam) <= 0) && ( bError == false ) )
{
MessageBox(hwnd,"Please enter an exam name!",0,0);
SetFocus(GetDlgItem(hwnd,SD_MERGE_EXAM));
bError = true;}
This way is simpler, clearer, and easier to follow mentally.
I understand your logic, but I need it the way it is. I will later be adding code into that empty if statement which will check for previous instances of the entered text. Though out of sheer baldness, I will test the change.
-
Sometimes it helps to rewrite the logic tests to be more affirmative. In the example you show, you're making use of what I call "testing for a negative" which can be very hard to follow. Try rewriting the tests like this:
if ( (strlen(exam) <= 0) && ( bError == false ) )
{
MessageBox(hwnd,"Please enter an exam name!",0,0);
SetFocus(GetDlgItem(hwnd,SD_MERGE_EXAM));
bError = true;}
This way is simpler, clearer, and easier to follow mentally.
This is just bizzare. Your advice works, as it should do, but then again, my code should also work. I also tried the following
if ((strlen(exam) > 0) && (!bError) ) {
int i = 0;
}
else if (!bError) {
MessageBox(hwnd,"Please enter an exam name!",0,0);
SetFocus(GetDlgItem(hwnd,SD_MERGE_EXAM));
bError = true;
}Which also works. So the ultimate question is, why the hell is an empty if statement causeing bError to become true?
-
This is just bizzare. Your advice works, as it should do, but then again, my code should also work. I also tried the following
if ((strlen(exam) > 0) && (!bError) ) {
int i = 0;
}
else if (!bError) {
MessageBox(hwnd,"Please enter an exam name!",0,0);
SetFocus(GetDlgItem(hwnd,SD_MERGE_EXAM));
bError = true;
}Which also works. So the ultimate question is, why the hell is an empty if statement causeing bError to become true?
Wow. If it suddenly starts working just with the addition of the
int i = 0;
, then I can't claim to know what is causing the problem. Perhaps the issue has to do with optimizations applied by the compiler. Try this: Go back to the original (non-working) code, and then go into the Project Properties, and turn OFF all compiler and linker optimizations. Then build it and see if it works. -
Wow. If it suddenly starts working just with the addition of the
int i = 0;
, then I can't claim to know what is causing the problem. Perhaps the issue has to do with optimizations applied by the compiler. Try this: Go back to the original (non-working) code, and then go into the Project Properties, and turn OFF all compiler and linker optimizations. Then build it and see if it works.I have played with all the project setting that I dare and nothing changes the fact that an empty
if
statement is causing me problems. You won't believe how embaressed I was today when my boss asked for a demonstration. The whole thing came to a standstill simply because data could not be merged. -
I have played with all the project setting that I dare and nothing changes the fact that an empty
if
statement is causing me problems. You won't believe how embaressed I was today when my boss asked for a demonstration. The whole thing came to a standstill simply because data could not be merged.I would really think that the compiler is doing this to you. Set the optimization to DEBUG on your release build and rebuild everything. Are you stepping through this with the debugger?