This is not a bug report. It is merely an observation, and a question to see if anyone else has run into this. My platform is Lazarus 2.0.10, FPC 3.2.0, Windows Server 2016. This exact same problem occurred in Delphi (years ago), and now Lazarus (yesterday).
Description of problem:
During run time, in the production environment, a bug appears, with error dialog displayed. Bug can be recreated consistently. Should be easy to find, just retrace steps. Open project in dev environment, Release mode, run, retrace steps, bug appears as expected. Close, switch compiler to Default mode, retrace steps, app works perfectly, no bug. Repeat this several times, ensuring there are no compiler directives that would remove or add lines of code between the Release and Default modes.
After several minutes, several iterations, it is determined that the only difference is the compiler mode. Source code is correct.
Cure:
Simply add at least ONE line of code, even if it does nothing. Recompile, in Release mode, bug goes away.
Conclusion:
Something in the compiler (Delphi or Lazarus) is skipping a line of source code, or, compiling it wrong.
In my Delphi project I had the following code:
if Length(s) > 5 then // <-- THIS LINE
begin
ShowMessage('I am greater than five');
end
The line marked THIS LINE would ALWAYS resolve to FALSE even if the length of s was more than five. The cure was to add a comment line above this section, like this.
// By adding this comment, above the IF, the line below started working
if Length(s) > 5 then // <-- THIS LINE
begin
ShowMessage('I am greater than five');
end
In the Lazarus example:
(in the database the AVL field is defined as NOT NULL with constraint IN('N','Y')
qryStock.ParamByName('STOCK_NUMBER').AsString := my_stock_nmbr;
qryStock.ParamByName('DSC').AsString := my_item_description;
qryStock.ParamByName('AVL').AsString := my_item_available; // <-- THIS LINE
... commit to database
In dev environment, compiling to Default mode, run, everything works. Compiling to Release mode, run, the line marked THIS LINE is skipped, the AVL parameter is left NULL and an error is displayed upon attempt to commit to database.
Cure:
qryStock.ParamByName('STOCK_NUMBER').AsString := my_stock_nmbr;
qryStock.ParamByName('DSC').AsString := my_item_description;
qryStock.ParamByName('AVL').AsString := my_item_available;
// I add this section AFTER the lines above
Screen.Cursor := crHourGlass;
Application.ProcessMessages;
try
... commit to database
finally
Screen.Cursor := crDefault;
Application.ProcessMessages;
end;
Now, this app works when compiled to Default mode OR Release mode. All I did was add a few lines of code, or a comment, which SHIFTED the lines of code, and changed the total line count, then everything worked reliably.
Now that I am aware of this, I am not stumped for hours trying to resolve it. If none of my code has changed, and there are no compiler directives that are used or changed between Default and Release modes... I just add a comment or a few lines of code, and the problem goes away.
This has happened to me once in Delphi and now twice in Lazarus, about a year or more apart.
Just wondering if anyone else has run into this, and what, if anything, I am doing wrong, or not looking for that I should be looking for.