Ok John I see now.
The compiler seems correct, there is a flaw in the logic of the function 'MainDlgProc'.
When you declare:
HICON hIcon = NULL;
or
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')
The problem is essentially related to the
scope of the variable. Probably this piece of code was originally positioned in the 'WinMain' function and its scope was persistent for the whole program execution ('WinMain' scope end when program ends). Moving the code to a function the scope was modified and local to the function itself.
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:
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.