Recent

Author Topic: Hint message "Local variable xx does not seem to be initialized" problem  (Read 17833 times)

GMB

  • Newbie
  • Posts: 2
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

  • Hero Member
  • *****
  • Posts: 11916
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #1 on: January 05, 2017, 07:28:12 pm »
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

  • Hero Member
  • *****
  • Posts: 2242
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #2 on: January 06, 2017, 10:16:28 am »
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-}".
If you don't like only this hint, add in the beginning
Code: Pascal  [Select][+][-]
  1. {$WARN 5057 OFF} // Hint: Local variable "..." does not seem to be initialized
or use framing in necessary places
Code: Pascal  [Select][+][-]
  1. {$PUSH}
  2. {$WARN 5057 OFF}
  3. // Some code
  4. {$POP}
This code (5057) I took from the context menu for Hint in the Messages window

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #3 on: January 06, 2017, 10:51:23 pm »
If you don't like only this hint, add in the beginning
Code: Pascal  [Select][+][-]
  1. {$WARN 5057 OFF} // Hint: Local variable "..." does not seem to be initialized

Very bad idea.

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


mtanner

  • Sr. Member
  • ****
  • Posts: 287
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #4 on: January 08, 2017, 09:53:00 am »
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.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #5 on: January 08, 2017, 10:31:14 am »
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.
This is exactly what the {%H-} directive does - read the last sentence in my post above.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #6 on: January 08, 2017, 12:05:11 pm »
If you're merely trying to initialize a variable to 0 using FillChar() you can use a new feature of FPC 3.0.0, namely the Default() intrinsic ( http://wiki.freepascal.org/FPC_New_Features_3.0#New_compiler_intrinsic_Default ):

Code: Pascal  [Select][+][-]
  1. SomeVariable := Default(TypeOfSomeVariable);

GMB

  • Newbie
  • Posts: 2
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #7 on: January 09, 2017, 11:38:49 am »
Thanks to the above replies I have some solutions that look acceptable. I had forgotten that old Delphi supported OUT so that works OK. The {%H-} also looks like a useful thing, although it is slightly dangerous in that something might move and the new message that I would have wanted to see gets hidden.

One day the developers might like to add initialization of stack variables (something I included in my Pascal-based S.I.L of a very long time ago). The whole area of initialization had been badly handled by Borland and I am not sure that this legacy has been fixed yet. I deeply dislike the way object initialization was handled - but that is not relevant here.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #8 on: January 09, 2017, 11:44:46 am »
[...] add initialization of stack variables [...]
At least manual initialization is already there:
Code: Pascal  [Select][+][-]
  1. procedure DoSomething;
  2. var
  3.   a: Integer = 0;
  4. begin
  5.  ....

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #9 on: January 09, 2017, 12:51:19 pm »
At least manual initialization is already there:
But not for var parameters, only value, const and constref parameters.
Code: Pascal  [Select][+][-]
  1. function DoSomething(var a:integer = 0);
will not work. The reasons are already explained.
« Last Edit: January 09, 2017, 12:53:19 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #10 on: October 28, 2022, 10:55:06 am »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6. procedure SomeMethod();
  7. var
  8.   a: SysUtils.TStringArray;
  9. begin
  10.   SetLength(a, 1);
  11.   a[0] := 'hello';
  12.  
  13.   Writeln(a[0]);
  14.  
  15.   a := nil;
  16. end;
  17.  
  18. begin
  19.   SomeMethod();
  20. end.    
  21.  

Also this generates the hint, to avoid I should either disable the hint or put initialization to nil before setting length.

Code: Pascal  [Select][+][-]
  1.   a := nil;
  2.  
  3.   SetLength(a, 1);
  4.   a[0] := 'hello';
  5.  
  6.   Writeln(a[0]);
  7.  
  8.   a := nil;
  9.  

No other way to avoid?

« Last Edit: October 28, 2022, 10:59:17 am by tt »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #11 on: October 28, 2022, 11:08:16 am »
The local variable in question is actually being initialized by calling a procedure with it as VAR parameter, which does indeed initialize it.

No it doesn't, the called procedure may exit before assigning anything.

If Delphi was assuming that that wouldn't happen, then /it/ was wrong. Note that this is a compile-time error, but in principle the identity of the called procedure isn't known until linkage time, and its precise behaviour isn't known until runtime.

Now I accept that this is irritating, and even more irritating is when a legacy declaration (e.g. that a procedure parameter is var when it should be out) causes it. Note that the IDE messages window has right-button operations to mark this as benign, and that this can be done on a per-case basis rather than simply telling the compiler to suppress all warnings of that type.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #12 on: October 28, 2022, 01:20:20 pm »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6. procedure SomeMethod();
  7. var
  8.   a: SysUtils.TStringArray;
  9. begin
  10.   SetLength(a, 1);
  11.   a[0] := 'hello';
  12.  
  13.   Writeln(a[0]);
  14.  
  15.   a := nil;
  16. end;
  17.  
  18. begin
  19.   SomeMethod();
  20. end.    
  21.  

Also this generates the hint, to avoid I should either disable the hint or put initialization to nil before setting length.

Code: Pascal  [Select][+][-]
  1.   a := nil;
  2.  
  3.   SetLength(a, 1);
  4.   a[0] := 'hello';
  5.  
  6.   Writeln(a[0]);
  7.  
  8.   a := nil;
  9.  

No other way to avoid?

In documentation https://www.freepascal.org/docs-html/current/ref/refsu14.html#x38-500003.3.1:

Quote
Dynamic arrays
As of version 1.1, Free Pascal also knows dynamic arrays: In that case the array range is omitted, as in the following example:

Type 
  TByteArray = Array of Byte;
When declaring a variable of a dynamic array type, the initial length of the array is zero. The actual length of the array must be set with the standard SetLength function, which will allocate the necessary memory to contain the array elements on the heap.
The following example will set the length to 1000:

Var 
  A : TByteArray; 
 
begin 
  SetLength(A,1000);
After a call to SetLength, valid array indexes are 0 to 999: the array index is always zero-based.

apparently the approach of declaring the array and then setting the length to something is the normal way, without extra ":= nil" or {%H-}.

Is this Hint senseful, in this case?
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

dbannon

  • Hero Member
  • *****
  • Posts: 2794
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #13 on: October 28, 2022, 01:29:45 pm »
tt, I suspect Mark answered the wrong question, not spotting how old it was.

Now, tt, in your first post, the first block of (complete) code generates the hint ?  Not for me !  Have you declared a different compiler mode perhaps ?

Davo

Code: Pascal  [Select][+][-]
  1. dbannon@dell:~/Pascal/CLI$ cat not_init.pas
  2. program Project1;
  3.  
  4. uses
  5.   SysUtils;
  6.  
  7. procedure SomeMethod();
  8. var
  9.   a: SysUtils.TStringArray;
  10. begin
  11.   SetLength(a, 1);
  12.   a[0] := 'hello';
  13.   Writeln(a[0]);
  14.   a := nil;
  15. end;
  16.  
  17. begin
  18.   SomeMethod();
  19. end.    
  20. dbannon@dell:~/Pascal/CLI$ fpc not_init.pas
  21. Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
  22. Copyright (c) 1993-2021 by Florian Klaempfl and others
  23. Target OS: Linux for x86-64
  24. Compiling not_init.pas
  25. Linking not_init
  26. 18 lines compiled, 0.1 sec
« Last Edit: October 28, 2022, 01:32:11 pm by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Чебурашка

  • Hero Member
  • *****
  • Posts: 568
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Hint message "Local variable xx does not seem to be initialized" problem
« Reply #14 on: October 28, 2022, 01:38:42 pm »
tt, I suspect Mark answered the wrong question, not spotting how old it was.

Now, tt, in your first post, the first block of (complete) code generates the hint ?  Not for me !  Have you declared a different compiler mode perhaps ?

Davo

Hey, thanks.

I did Lazarus> New Project > Simple Progam.
Then I wrote the listed code and I get this notification.

Seems syntax mode is Object Pascal - default (see attach)

In reality, I'd say that the question is rather if this hint makes sense at all for a code like this, regardless of the settings about the Hints silencing ({%H-}, {$warn 5091 off} or -vm5091).
« Last Edit: October 28, 2022, 01:43:13 pm by tt »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

 

TinyPortal © 2005-2018