Recent

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

wp

  • Hero Member
  • *****
  • Posts: 8568
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.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

lucamar

  • Hero Member
  • *****
  • Posts: 4150
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. :)
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: 389
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 ..
usually using latest Lazarus release version with Windows 10

wp

  • Hero Member
  • *****
  • Posts: 8568
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.   ...
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

ASerge

  • Hero Member
  • *****
  • Posts: 1817
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