Pelles C > General discussions

Compiler Warning

(1/2) > >>

John Z:
if you declare

--- Code: ---HICON hIcon = NULL;
--- End code ---

then within

--- Code: --- case WM_INITDIALOG:
   hIcon = LoadImage( ghInstance,MAKEINTRESOURCE(JGZ),IMAGE_ICON,
                           32,32,LR_SHARED);
--- End code ---
                           
then within

--- Code: --- case WM_CLOSE:
   if (hIcon) {DestroyIcon(hIcon);}
--- End code ---
        
The compiler generates a Warning message for > 'if (hIcon) {DestroyIcon(hIcon);}'
: warning #2154: Unreachable code.
 
Then change the declare to

--- Code: ---HICON hIcon;
--- End code ---

The compiler generates a Warning message for > 'HICON hIcon;'
:warning #2229: Local 'hIcon' is potentially used without being initialized.

Is there a third way that has no warning ?  I use warnings Level 2 and C11.

John Z

frankie:
There is something wrong here.
You should get the message:
--- Code: ---: warning #2154: Unreachable code.
--- End code ---
because the compiler evaluates that the the condition:
--- Code: ---hIcon != NULL
--- End code ---
is never met.
For this reason the whole block:
--- Code: ---if (hIcon) {DestroyIcon(hIcon);}
--- End code ---
is removed from compilation output as "dead code".
This could happen if:

* hicon is a local variable
* No change happens to its value so the compiler can safely assume that it will always be null.To understand if these conditions are really met, and this is not a compiler bug, you should post the smallest piece of complete code that shows the problem.

If tis is a bug you can try as workaround:

--- Code: ---volatile HICON hIcon;
--- End code ---
The 'volatile' qualifier informs the compiler that the variable can be changed by external causes, and for this reason no assumptions on its value have to be made.

John Z:
Yes I understand why, but one or the other should not be in error I think.

static HICON hIcon = NULL;

Also 'fixes' (no warnings) it but clearly not that desirable.

I'll try to generate a simple example. 

John Z

frankie:

--- Quote from: John Z on March 31, 2022, 02:40:57 AM ---I'll try to generate a simple example. 

John Z

--- End quote ---
Yes please, because I can't reproduce such error with PellesC V11.

John Z:
Frankie, Thanks for the reminder to include the Pelles C version too.

 Initial work was from version 10.  I'm a slow adopter  :)

However attached is a simple program test case which I ran in version 10 and version 11.  The reported
warnings are somewhat different from each but both versions show the only way to satisfy the compiler (ie no warning)
is to use 'static HICON hIcon;' or 'volatile HICON hIcon = NULL;'

IMO should not be the case because clearly
--- Code: ---hIcon = LoadImage( ghInstance,MAKEINTRESOURCE(JGZ),IMAGE_ICON,
                           32,32,LR_SHARED);
--- End code ---
will affect the value of hIcon.

Just uncomment the variable declare at the top of MainDlgProc and compile.  The warning line number I put in the comments will be off a bit because I added comments while testing.  There are 5 test cases.  Should have been 6
I forgot to include 'static HICON hIcon = NULL;'


John Z

Navigation

[0] Message Index

[#] Next page

Go to full version