Recent

Author Topic: Hint: Local variable xx of a managed type does not seem to be initialized  (Read 8350 times)

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
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.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Hint: Local variable xx of a managed type does not seem to be initialized
« Reply #1 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!
« Last Edit: February 06, 2019, 12:44:20 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: Hint: Local variable xx of a managed type does not seem to be initialized
« Reply #2 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.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Hint: Local variable xx of a managed type does not seem to be initialized
« Reply #3 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 ...
« Last Edit: February 06, 2019, 01:10:51 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: Hint: Local variable xx of a managed type does not seem to be initialized
« Reply #4 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
« Last Edit: February 06, 2019, 01:53:30 pm by Igor Kokarev »

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: Hint: Local variable xx of a managed type does not seem to be initialized
« Reply #5 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.  

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Hint: Local variable xx of a managed type does not seem to be initialized
« Reply #6 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: Hint: Local variable xx of a managed type does not seem to be initialized
« Reply #7 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".


Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: Hint: Local variable xx of a managed type does not seem to be initialized
« Reply #8 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.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Hint: Local variable xx of a managed type does not seem to be initialized
« Reply #9 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?"
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Hint: Local variable xx of a managed type does not seem to be initialized
« Reply #10 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)
« Last Edit: February 06, 2019, 03:56:30 pm by Thaddy »
Specialize a type, not a var.

netkiller

  • New Member
  • *
  • Posts: 12
in most cases, this hint is useless, hope user can close it in project config in next lazaurs version.

PeterX

  • Sr. Member
  • ****
  • Posts: 404
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);    ??
usually using latest Lazarus release version with Windows 10

lucamar

  • Hero Member
  • *****
  • Posts: 4219
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 ..." ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PeterX

  • Sr. Member
  • ****
  • Posts: 404
Okay, this VAR is really not initialized ..
usually using latest Lazarus release version with Windows 10

 

TinyPortal © 2005-2018