91
General discussions / Re: Compiler Warning
« Last post by frankie on March 31, 2022, 05:45:23 pm »Ok John I see now.
The compiler seems correct, there is a flaw in the logic of the function 'MainDlgProc'.
When you declare:
So you can have:
Making it 'static' guarantee that the variable exists and retain its value between calls, miming the functionality as when declared in the 'WinMain'.
Anyway something that I've not fully understand happens using the 'volatile' qualifier.
Using:
Anyway making it 'static' no more complains are made.
The compiler seems correct, there is a flaw in the logic of the function 'MainDlgProc'.
When you declare:
Code: [Select]
HICON hIcon = NULL;
or Code: [Select]
HICON hIcon;
You are creating an automatic variable that will be lost as soon as execution flow exits the function. This imply that the value of the variable 'hicon' exists for a single switch case per call.So you can have:
- When 'uMsg' is 'WM_INITDIALOG' you are assigning a value to it, that will be lost on return.
- When 'uMsg' is 'WM_CLOSE':
- If 'hicon' isn't initialized you're using a dangling value for the 'if' test (warning #2116)
- if 'hicon' is initialized to NULL the comparison is constant and always false and the conditional code never executed ('warning #2154: Unreachable code' and 'warning #2154: Unreachable code')
Making it 'static' guarantee that the variable exists and retain its value between calls, miming the functionality as when declared in the 'WinMain'.
Anyway something that I've not fully understand happens using the 'volatile' qualifier.
Using:
Code: [Select]
volatile HICON hIcon = NULL;
the compiler doesn't complain, while omitting the initialization to 'NULL' V11 warns about constant compare. What's the rationale behind this?Anyway making it 'static' no more complains are made.