Lazarus

Free Pascal => General => Topic started by: Igor Kokarev on February 05, 2019, 03:09:36 pm

Title: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: Igor Kokarev on February 05, 2019, 03:09:36 pm
After updating to Lazarus 2.0.0 I found a new type of Hint messages:

"Hint: Local variable xx of a managed type does not seem to be initialized"

This hint didn't appear in previous v1.8.4.

I didn't find an option in Messages of a project options to hide this hint message.

For my project I consider this hint useless.
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: lucamar on February 05, 2019, 06:00:40 pm
That message was present also in Lazarus 1.8.x (and IIRC in 1.6.x). It appears, for example, in code like:

Code: Pascal  [Select][+][-]
  1. {deleted, see example below}

It's normally useless but not always. That's (I suppose) why it is a hint rather than a warning.

Never seen it before? Lucky you!  :)

ETA: One example (Lazarus/FPC 1.8.4/3.0.4)
Code: Bash  [Select][+][-]
  1. lucamar@selene:~/SoftDev/Minipad$ lazbuild minipad.lpi
  2. Info: (lazarus) Execute Title="Compile Project, Mode: Release, Target: minipad"
  3. Info: (lazarus) Working Directory="/home/lucamar/SoftDev/Minipad/"
  4. {... pruned some lines ...}
  5. {***** See? This is Lazarus 1.8.4 *****}
  6. Info: (lazarus) Param[9]="-Fu/usr/share/lazarus/1.8.4/lcl/units/i386-linux/gtk2"
  7. {... pruned some more lines ...}
  8. Info: (lazarus) Param[17]="minipad.lpr"
  9. Hint: (11030) Start of reading config file /etc/fpc.cfg
  10. Hint: (11031) End of reading config file /etc/fpc.cfg
  11. Free Pascal Compiler version 3.0.4 [2018/05/21] for i386
  12. Copyright (c) 1993-2017 by Florian Klaempfl and others
  13. (1002) Target OS: Linux for i386
  14. (3104) Compiling minipad.lpr
  15. (3104) Compiling fmain.pas
  16. {***** HERE is one (nonsense) "not initialized" hint *****}
  17. /home/lucamar/SoftDev/Minipad/fmain.pas(250,12) Hint: (5092) Variable "FindStr" of a managed type does not seem to be initialized
  18. /home/lucamar/SoftDev/Minipad/fmain.pas(98,26) Hint: (5024) Parameter "Sender" not used
  19. {... pruned lots of these kind of hint ...}
  20. /home/lucamar/SoftDev/Minipad/fmain.pas(120,28) Hint: (5024) Parameter "Sender" not used
  21. (9022) Compiling resource /home/lucamar/SoftDev/Minipad/lib/i386-linux/minipad.or
  22. (9015) Linking minipad
  23. /usr/bin/ld: warning: link.res contains output sections; did you forget -T?
  24. (1008) 825 lines compiled, 18.0 sec
  25. (1022) 21 hint(s) issued

That line is inside a function declared as:
Code: Pascal  [Select][+][-]
  1. function TMain.FindInMemo(FindStr: String; var FindFrom: Integer;
  2.   Options: TFindOptions = DefFindOptions): Boolean;

The line itself is just
Code: Pascal  [Select][+][-]
  1.   { "Match case" uses both texts as they are; if not ... }
  2.   if not (frMatchCase in Options) then begin
  3.     SrcText := LowerCase(SrcText);
  4.     FindStr := LowerCase(FindStr);
  5.   end;
  6.  

The hint is non-sense because I have already tested whether FindStr = ''; so to avoid seeing it (in Lazarus only!) I just add a {%H-} before the line:
Code: Pascal  [Select][+][-]
  1.   if not (frMatchCase in Options) then begin
  2.     SrcText := LowerCase(SrcText);
  3.     {%H-}FindStr := LowerCase(FindStr);
  4.   end;
  5.  
and done!
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: ASerge on February 05, 2019, 11:25:55 pm
It appears, for example, in code like:
Code: Pascal  [Select][+][-]
  1. procedure Something(var AString: AnsiString);
  2. begin
  3.   if AString = '' then {<--- This gives the hint}
  4.     AString := DoX(AString)
  5.   else
  6.     AString := AString + DoX(AString);
  7. end;
Are you sure?
This code compiles without any hints, even with -Oodfa
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2.  
  3. procedure Something(var AString: AnsiString);
  4. begin
  5.   if AString = '' then
  6.     AString := AString + '1'
  7.   else
  8.     AString := AString + '2';
  9. end;
  10.  
  11. var
  12.   S: AnsiString;
  13. begin
  14.   S := '';
  15.   Something(S);
  16. end.
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: lucamar on February 06, 2019, 12:51:43 am
<snip>
Are you sure?
<snip>

Yes, although I marked the wrong line; the hint goes to the next: AString := DoX(AString) and my guess is it's about the call to DoX(AString), as can be seen in the second, real-life example I added. However that first example you cite is not up to par so I've deleted it from my post.

Well, WTH? I just tested with this simple program:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses sysutils;
  6.  
  7. var sv: String;
  8.  
  9. function DoX(AString: String): String;
  10. begin
  11.   Result := LowerCase(AString);
  12. end;
  13.  
  14. procedure Something(var AString: String);
  15. begin
  16.   if AString = '' then
  17.     AString := DoX(AString)
  18.   else
  19.     AString := AString + ' = ' + DoX(AString);
  20. end;
  21.  
  22. begin
  23.   { Pass an empty string }
  24.   sv := '';
  25.   Something(sv);
  26.   writeln(sv);
  27.   { Pass a non-empty string }
  28.   sv := 'I''m just a simple string...';
  29.   Something(sv);
  30.   writeln(sv);
  31. end.

and you're right: it doesn't generate any hint whatsoever.
:-[

Anyway, the point was that it's not a new hint ...
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: Igor Kokarev on February 06, 2019, 01:11:12 pm
Hi,

Thanks for your reply. I agree that these hints can be useful.

However the problem in a fact that it is NOT possible to hide this hint (5091) from a popup menu in Lazarus in Messages window.

Edited: See new message below.

I choose "Filter all messages of type: Hint (5091) variable "$1" of a managed type does not seem to be initialized"

and nothing happens, I still see these hints in Messages window, even if I restart Lazarus.

Also this hint doesn't exist in Project Options > Messages.

The only way to hide hint 5091 is to add an option -vm5091 in Project Options > Custom Options
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: Igor Kokarev on February 06, 2019, 01:28:58 pm
According this article:
https://www.freepascal.org/docs-html/ref/refse20.html
Managed types already initialized by default (nil).

For example, I create a local variable WideString and pass it as var variable to another function.
It should be initialized as nil by default. However Lazarus 2.0 generates:

"project1.lpr(18,12) Hint: Local variable "Str" of a managed type does not seem to be initialized"

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes;
  7.  
  8. procedure Test2(var Str: WideString);
  9. begin
  10.   Str:='wegweg';
  11. end;
  12.  
  13. procedure Test1;
  14. var
  15.   Str:    WideString;
  16. begin
  17.   Test2(Str);
  18.   WriteLn(Str);
  19. end;
  20.  
  21. begin
  22.   Test1;
  23.   ReadLn;
  24. end.
  25.  
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: lucamar on February 06, 2019, 01:44:19 pm
You're right; the only thing that works is "Hide message by inserting IDE directive {$H-}". In fact, selecting to insert "{$warn 5092 off}" really inserts "{$warn 5092 on}" instead.

It's easy to change that "on" to "off" but it's very clearly a bug. You should report it in the bug tracker.
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: Igor Kokarev on February 06, 2019, 01:57:38 pm
If I call the popup menu from Messages window and call "Hide with project option (-vm5091)". It does NOT work.

I have to add manually -vm5091 the Custom Options.

P.S. Another minor problem in Messages window. I see 2 hints:

Hint: Start of reading config file C:\lazarus64\fpc\3.0.4\bin\x86_64-win64\fpc.cfg
Hint: End of reading config file C:\lazarus64\fpc\3.0.4\bin\x86_64-win64\fpc.cfg

Althought I have default mode "Filter Verbose Messages and Below".

Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: Igor Kokarev on February 06, 2019, 02:00:07 pm
You're right; the only thing that works is "Hide message by inserting IDE directive {$H-}". In fact, selecting to insert "{$warn 5092 off}" really inserts "{$warn 5092 on}" instead.
Yes, but it works only for one unit. We have many units where this hints occur.

As I wrote earlier, this hint is useless, because managed types are initialized by default by FPC.
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: lucamar on February 06, 2019, 02:13:14 pm
As I wrote earlier, this hint is useless, because managed types are initialized by default by FPC.

Take the String type, for example: An empty string is initialized to Nil. If you then try to access the characters as if it were an array, say with something like:
Code: Pascal  [Select][+][-]
  1. if MyString[1] = 'A' then ...
the result will be an exception. The hint, quite properly, simply tells you that maybe you forgot that possibility.

Most hints are of that kind: they attract your attention to something that may need some intervention. If you're usually careful then most of them seem useless ... until that time when you were not careful enough. An example, with other hint that you've probably seen hundred of times, the one saying: "Unused parameter 'Sender'". Mostly useless ... until you see instead "Unused Parameter 'AnotherThing'"; then you know something may be wrong in your code.

That's what hints are for: it's the compiler's way of saying "Excuse me but, are you sure this is what you meant?"
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: Thaddy on February 06, 2019, 03:53:33 pm
Maybe it is an idea to make -Sew (treat warnings as errors) the default.....
In this case the point is it should be a warning and not a hint..... (That's a minor bug, so report it)
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: netkiller on April 29, 2019, 09:28:58 am
in most cases, this hint is useless, hope user can close it in project config in next lazaurs version.
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: PeterX on May 09, 2021, 10:41:57 am
That's what hints are for: it's the compiler's way of saying "Excuse me but, are you sure this is what you meant?"

Same case, example (extracted from a foreign unit - I did not write it) :

Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. var
  3.   x : Array of integer;
  4. begin
  5.   SetLength( x, 0);
  6.   ...
  7.   ...
  8. end;
  9.  

And the compiler says:
Code: Pascal  [Select][+][-]
  1. unit1.pas(29,15) Hint: [b]Local variable "x" of a managed type does not seem to be initialized[/b]
  2.  

Indeed, 'SetLength()'  looks wrong to me, I would have written ..
Code: Pascal  [Select][+][-]
  1.   x:= NIL;
  2.  
.. to init this local var.


But why is this 'bad' code:   SetLength( x, 0);    ??
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: lucamar on May 09, 2021, 11:09:00 am
But why is this 'bad' code:   SetLength( x, 0);    ??

It isn't, in this particular case, but think of what the compiler is seeing: You're passing to a procedure a (var) parameter that (maybe) you forgot to initialize, so "excuse me, are you sure ..." ;)
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: PeterX on May 09, 2021, 12:29:23 pm
Okay, this VAR is really not initialized ..
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: wp on May 09, 2021, 01:30:50 pm
My standard way to get rid of this hint is to initialize the variable in the declaration:
Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. var
  3.   x : Array of integer = nil;
  4.   s: string = '';
  5.   ws: widestring = '';
  6. begin
  7.   SetLength(x, ...);
  8.   SetLength(s, ...);
  9.   SetLength(ws, ...);
  10.   ...
  11.   ...
  12. end;
These are very natural and self-explaining additions and much clearer than the standard {%H} directive.
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: lucamar on May 09, 2021, 01:57:12 pm
My standard way to get rid of this hint is to initialize the variable in the declaration: [...]

Yes, I do the same. The code looks much cleaner than using directives. :)
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: PeterX on May 09, 2021, 11:13:00 pm
My standard way to get rid of this hint is to initialize the variable in the declaration:
Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. var
  3.   x : Array of integer = nil;
  4.   s: string = '';
  5.   ws: widestring = '';
  6. begin
  7.   SetLength(x, ...);
  8.   SetLength(s, ...);
  9.   SetLength(ws, ...);
  10.   ...
  11.   ...
  12. end;
These are very natural and self-explaining additions and much clearer than the standard {%H} directive.

Yes, this is self-explaining, and done at the root. Thumbs up !

But I today found some local vars that are records.
And I get this HINT ".. of a managed type does not seem to be initialized".
I have no idea how to solve this in a clean way ..
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: wp on May 09, 2021, 11:48:18 pm
You can call Default() at the top of the routine (https://wiki.freepascal.org/FPC_New_Features_3.0.0#New_compiler_intrinsic_Default):

Code: Pascal  [Select][+][-]
  1. type
  2.   TData = record
  3.     a, b: Integer;
  4.   end;
  5.  
  6. procedure Test;
  7. var
  8.   data: TData;
  9. begin
  10.   data := Default(TData);
  11.   ...
Title: Re: Hint: Local variable xx of a managed type does not seem to be initialized
Post by: ASerge on May 10, 2021, 03:50:09 am
Code: Pascal  [Select][+][-]
  1. procedure Test;
  2. var
  3.   x : Array of integer = nil;
  4.   s: string = '';
  5.   ws: widestring = '';
  6. begin
  7.   SetLength(x, ...);
  8.   SetLength(s, ...);
  9.   SetLength(ws, ...);
  10.   ...
  11.   ...
  12. end;
By the way, this code will be without a hint, even if there is no initialization.
Sample project:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3.  
  4. procedure UseVariable(var V);
  5. begin
  6.   Writeln(HexStr(@V));
  7. end;
  8.  
  9. procedure Test1Hint;
  10. var
  11.   A: TBoundArray;
  12. begin
  13.   UseVariable(A); // project1.lpr(13,16) Hint: Local variable "A" of a managed type does not seem to be initialized
  14. end;
  15.  
  16. procedure Test1_1NoHint;
  17. var
  18.   A: TBoundArray;
  19. begin
  20.   SetLength(A, 10); // Set the length first (and there is no hint in this line!)
  21.   UseVariable(A);   // No hint
  22. end;
  23.  
  24. procedure Test1_2NoHint;
  25. var
  26.   A: TBoundArray = nil; // Initialize in the declaration
  27. begin
  28.   UseVariable(A); // No hint
  29. end;
  30.  
  31. procedure Test2Hint;
  32. var
  33.   R: record
  34.     A: TBoundArray;
  35.   end;
  36. begin
  37.   UseVariable(R.A); // project1.lpr(37,15) Hint: Local variable "R" of a managed type does not seem to be initialized
  38. end;
  39.  
  40. procedure Test2_1NoHint;
  41. var
  42.   R: record
  43.     A: TBoundArray;
  44.   end;
  45. begin
  46.   SetLength(R.A, 10); // Set the length first
  47.   UseVariable(R.A); // No hint
  48. end;
  49.  
  50. procedure Test2_2NoHint;
  51. var
  52.   R: record
  53.     A: TBoundArray;
  54.   end;
  55. begin
  56.   R.A := nil; // Initialize explicitly
  57.   UseVariable(R.A); // No hint
  58. end;
  59.  
  60. procedure Test2_3NoHint;
  61. type
  62.   TR = record
  63.     A: TBoundArray;
  64.   end;
  65. var
  66.   R: TR;
  67. begin
  68.   R := Default(TR); // Initialize with the Default
  69.   UseVariable(R.A); // No hint
  70. end;
  71.  
  72. begin
  73.   Test1Hint;
  74.   Test1_1NoHint;
  75.   Test1_2NoHint;
  76.   Test2Hint;
  77.   Test2_1NoHint;
  78.   Test2_2NoHint;
  79.   Test2_3NoHint;
  80.   ReadLn;
  81. end.
TinyPortal © 2005-2018