Recent

Author Topic: Default, Manual Initialize, or Automatic Initialize  (Read 1497 times)

Okoba

  • Hero Member
  • *****
  • Posts: 648
Re: Default, Manual Initialize, or Automatic Initialize
« Reply #15 on: January 21, 2026, 09:17:22 am »
@PascalDragon what about TestManagedRecordParent? If it has other properties, they will be left uninitialized, and I have two way, either add Initialize for every record that uses it, or use Default and it cancels the manage record Initialize.
Is there a distinction between this warn and others so I can disable these only? I try to not have such errors in my code to prevent bugs, but ignoring compiler warn seems risky :)

@Khrys thank you for the clarification. Records are great but this manage part needs some improvement.

Okoba

  • Hero Member
  • *****
  • Posts: 648
Re: Default, Manual Initialize, or Automatic Initialize
« Reply #16 on: February 01, 2026, 08:41:03 pm »
This topic is still not resolved to me, please add your thoughts.

Khrys

  • Sr. Member
  • ****
  • Posts: 391
Re: Default, Manual Initialize, or Automatic Initialize
« Reply #17 on: February 02, 2026, 08:01:20 am »
Is there a distinction between this warn and others so I can disable these only?

Yes, there's a four-way distinction to these messages: managed vs unmanaged, local vs global, function result vs non-function result, warning vs hint:


5036 WARN Local variable does not seem to be initialized
5037 WARN Variable does not seem to be initialized
5057 HINT Local variable does not seem to be initialized
5058 HINT Variable does not seem to be initialized
5059 WARN Function result variable does not seem to be initialized
5060 HINT Function result variable does not seem to be initialized

5089 WARN Local variable of a managed type does not seem to be initialized
5090 WARN Variable of a managed type does not seem to be initialized
5091 HINT Local variable of a managed type does not seem to be initialized
5092 HINT Variable of a managed type does not seem to be initialized
5093 WARN Function result variable of a managed type does not seem to be initialized
5094 HINT Function result variable of a managed type does not seem to be initialized


I try to not have such errors in my code to prevent bugs, but ignoring compiler warn seems risky :)

The only circumstance in which these messages carry any weight is when the respective managed type does not implement  class operator Initialize,  so just make sure to always provide it.

Okoba

  • Hero Member
  • *****
  • Posts: 648
Re: Default, Manual Initialize, or Automatic Initialize
« Reply #18 on: February 02, 2026, 08:15:44 am »
You mean I need to implement Initialize operator for all my records?

Khrys

  • Sr. Member
  • ****
  • Posts: 391
Re: Default, Manual Initialize, or Automatic Initialize
« Reply #19 on: February 02, 2026, 08:31:26 am »
You mean I need to implement Initialize operator for all my records?

Only for your managed record types. If all managed record types that you use have that operator, then the aforementioned warnings can be safely ignored by switching them off (i.e. at the project level).

For example, in the following case, the warning will appear and it will be justified because there is no  Initialize  operator:

Code: Pascal  [Select][+][-]
  1. type
  2.   TAutoFree = record
  3.     Data: Pointer;
  4.     class operator Finalize(var Self: TAutoFree);
  5.   end;
  6.  
  7. class operator TAutoFree.Finalize(var Self: TAutoFree);
  8. begin
  9.   FreeMem(Self.Data);
  10.   Self.Data := Nil;
  11. end;
  12.  
  13. procedure Foo();
  14. var
  15.   Buffer: TAutoFree;
  16. begin
  17.   // `Buffer` wasn't initialized -> CRASH AND BURN on `FreeMem`
  18. end;
  19.  

However, if you make sure to implement  Initialize,  then the warning is meaningless (even though it still appears):

Code: Pascal  [Select][+][-]
  1. type
  2.   TAutoFree = record
  3.     Data: Pointer;
  4.     class operator Initialize(var Self: TAutoFree);
  5.     class operator Finalize(var Self: TAutoFree);
  6.   end;
  7.  
  8. class operator TAutoFree.Initialize(var Self: TAutoFree);
  9. begin
  10.   Self.Data := Nil;
  11. end;
  12.  
  13. class operator TAutoFree.Finalize(var Self: TAutoFree);
  14. begin
  15.   FreeMem(Self.Data);
  16.   Self.Data := Nil;
  17. end;
  18.  
  19. procedure Foo();
  20. var
  21.   Buffer: TAutoFree;
  22. begin
  23.   // `Buffer.Data` is set to `Nil` -> `FreeMem` does nothing
  24. end;
  25.  

440bx

  • Hero Member
  • *****
  • Posts: 6073
Re: Default, Manual Initialize, or Automatic Initialize
« Reply #20 on: February 02, 2026, 09:15:24 am »

5036 WARN Local variable does not seem to be initialized
5037 WARN Variable does not seem to be initialized
5057 HINT Local variable does not seem to be initialized
5058 HINT Variable does not seem to be initialized
5059 WARN Function result variable does not seem to be initialized
5060 HINT Function result variable does not seem to be initialized

Generally speaking I think it is "prudent" to write code in such a way that _none_ of the above hints and warnings are emitted by the compiler.

Even something as simple as a local variable not being initialized can be the cause of a hard to find bug because the value of the local variable is unpredictable.

I think it is simply good programming to _always_ initialize _all_ variables.

I think the bad habit of not initializing all variables as a matter of course comes from the obsession of making code as fast as possible.  I think an obsession of making the code _correct_ is a much more useful and justified obsession.  That said, I stopped using FPC's "= ();" to initialize records because of the outrageously bad code it generates, now I just ZeroMemory the things.

As far as initializing records, I believe that every record should have an "Init" procedure that sets _all_ of its fields to appropriate default values.  Here, I have to admit that while I more often than not do that, there are times when I skip that step (usually for code I consider "not important".)  I mention this because I don't want to appear to be one of those "do as I say, not as I do".

Anyway, initializing variables only costs a few clock cycles, in today's 4 to 6 Ghz machines, it would really take a lot of initializations to make a perceptible difference in the execution time but, it can definitely make a very perceptible difference in the amount of time spent (not) debugging the program.

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Khrys

  • Sr. Member
  • ****
  • Posts: 391
Re: Default, Manual Initialize, or Automatic Initialize
« Reply #21 on: February 02, 2026, 09:32:16 am »

5036 WARN Local variable does not seem to be initialized
5037 WARN Variable does not seem to be initialized
5057 HINT Local variable does not seem to be initialized
5058 HINT Variable does not seem to be initialized
5059 WARN Function result variable does not seem to be initialized
5060 HINT Function result variable does not seem to be initialized

Generally speaking I think it is "prudent" to write code in such a way that _none_ of the above hints and warnings are emitted by the compiler.

I absolutely agree. I just included those to demonstrate the distinction between managed and non-managed types and that it's possible to selectively ignore only the warnings related to the former.

Okoba

  • Hero Member
  • *****
  • Posts: 648
Re: Default, Manual Initialize, or Automatic Initialize
« Reply #22 on: February 02, 2026, 09:36:49 pm »
I agree that values should be initialized and that is why I care about the warnings. My problem is that showing warning for records with Initialize operator makes the code dangerous as you only can ignore them by eye or by conditioning the compiler and both make you miss the really missed ones.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Default, Manual Initialize, or Automatic Initialize
« Reply #23 on: February 04, 2026, 04:26:30 pm »
instead of the initialize operator you could also call the global initialize() function on it, depending on use.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Okoba

  • Hero Member
  • *****
  • Posts: 648
Re: Default, Manual Initialize, or Automatic Initialize
« Reply #24 on: February 05, 2026, 10:48:51 am »
It will cause double initialize, one automatic by the compiler and one manual by calling initialize().
Honestly I do not know if I am confused with this current situation or the FPC record design is confusing/wrong.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Default, Manual Initialize, or Automatic Initialize
« Reply #25 on: February 05, 2026, 03:06:23 pm »
In the case I mentioned of course do not write management operators for the record.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

 

TinyPortal © 2005-2018