Forum > General

Hint message "Local variable xx does not seem to be initialized" problem

(1/10) > >>

GMB:
I am trying to cut over from old Delphi-7 to Lazarus.
I like compiler hint messages as they often warn of silly mistakes and with Delphi I make everything compile without a single complaint.
But I am disappointed to find that with Lazarus I get the "not initialized" message for cases where it is wrong and Delphi would not have said this.

The local variable in question is actually being initialized by calling a procedure with it as VAR parameter, which does indeed initialize it.
I can see why it might be rather hard for the compiler to work this out properly, but the Delphi solution of "it's a VAR usage so assume it got initialized" approach is way more helpful.

I can only see an option to turn the whole feature off. Am I missing something here?

I sometimes have had to put in gratuitous code to shut Delphi up, but in this case it is less easy as the whole point is that it is the code to initialize a (maybe big) record that gets the complaint - so I don't see a work round.

wp:
Although it may be annoying sometimes, the hint is a valid one. A var parameter of a procedure is an input/output parameter. The compiler cannot tell if it is for input or output. If the procedure uses it as an input parameter then it must be initialized, of course. And this is what the hint is telling you: there is some risk that the parameter is not initialized.

If the parameter is for output only then declare it as "out" (instead of "var"). Then the compiler knows that it does not need initialization, and the hint will go away.

Sometimes the hint appears for methods which are inherited from somewhere else where the parameter type cannot be changed to "out". In this case, there is a brute-force solution by right-clicking on the message and selecting "Hide message by inserting IDE directive {%H-}".

ASerge:

--- Quote from: wp on January 05, 2017, 07:28:12 pm ---Sometimes the hint appears for methods which are inherited from somewhere else where the parameter type cannot be changed to "out". In this case, there is a brute-force solution by right-clicking on the message and selecting "Hide message by inserting IDE directive {%H-}".

--- End quote ---
If you don't like only this hint, add in the beginning

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$WARN 5057 OFF} // Hint: Local variable "..." does not seem to be initializedor use framing in necessary places

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$PUSH}{$WARN 5057 OFF}// Some code{$POP}This code (5057) I took from the context menu for Hint in the Messages window

BeniBela:

--- Quote from: ASerge on January 06, 2017, 10:16:28 am ---If you don't like only this hint, add in the beginning

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$WARN 5057 OFF} // Hint: Local variable "..." does not seem to be initialized
--- End quote ---

Very bad idea.

Usually when I ignore the hint, it turns out to be a severe bug later

mtanner:
I sympathise with this. I find myself putting code immediately at the start of a procedure to initialise all local variables. I too like to get all hints removed, but not by suppressing them because they may indeed signify a programming error. One possibility would be to invent a new FPC statement, something like

NoInit alpha,I,X;

meaning that you list the variables which do not require initialisation, so no warning message is issued, but no actual code is generated.

Meanwhile I will just go on putting alpha:=0; I:=0; X:=0; at the start of the procedure.

Navigation

[0] Message Index

[#] Next page

Go to full version