Recent

Author Topic: Things go bad when not using large strings while in objfpc mode.  (Read 619 times)

jamie

  • Hero Member
  • *****
  • Posts: 6988
case and point.

 I had some old ported D code that used {$Mode Delph} with no  {$H+} afterwards.
 
 However, switch to {$Mode ObjFpc}// no {$H+} afterwards and look at the following code example that you can try out.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}//Things go bad without {$H+} in objfpc mode.
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     DrawGrid1: TDrawGrid;
  16.     procedure DrawGrid1GetCellHint(Sender: TObject; ACol, ARow: Integer;
  17.       var HintText: String);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.DrawGrid1GetCellHint(Sender: TObject; ACol, ARow: Integer;
  34.   var HintText: String);
  35. begin
  36.   HintText := DateTimeTostr(Now);
  37. end;
  38.  
  39. end.
  40.  
  41.  
Pay attention to the GetCellHint, there is when the crash takes place as soon as you assign the HintText with anything.
 
 make sure you have the goCellHint in options and ShowHint checked in the OI.

 This leads you to all kinds of random places each time you run it.
 
  This does not happen while in {$Mode Delphi} with or without the {$H+} so I assume Delphi mode turns it on by default.

jamie
The only true wisdom is knowing you know nothing

cdbc

  • Hero Member
  • *****
  • Posts: 2245
    • http://www.cdbc.dk
Re: Things go bad when not using large strings while in objfpc mode.
« Reply #1 on: June 15, 2025, 10:13:06 pm »
Hi
IIRC, long strings are the default in {$mode delphi}, whereas the default 'string' in {$mode objfpc} is _shortstring_, hence the need for {$H+}, 'cause in your example the "var HintText: String" is indeed in the LCL declared as a long string, thus the compiler whines about "a var parameter has to match the type exactly" or something like that...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: Things go bad when not using large strings while in objfpc mode.
« Reply #2 on: June 15, 2025, 10:13:39 pm »
Well, the event signature requires an AnsiString, so you assigned an event with the wrong signature.
IMO the IDE should warn about it, but ATM it cannot.
(There are other exapmples about this around there somewhere in teh bugtracker.)
You will see the error if you try to assign the event at runtime.

Bart

jamie

  • Hero Member
  • *****
  • Posts: 6988
Re: Things go bad when not using large strings while in objfpc mode.
« Reply #3 on: June 15, 2025, 10:24:19 pm »
Hi
IIRC, long strings are the default in {$mode delphi}, whereas the default 'string' in {$mode objfpc} is _shortstring_, hence the need for {$H+}, 'cause in your example the "var HintText: String" is indeed in the LCL declared as a long string, thus the compiler whines about "a var parameter has to match the type exactly" or something like that...
Regards Benny

There is no complaint from the compiler, it compiles and executes just fine, it's when you move the mouse over the grid is when the fun starts!

Jamie
The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5575
    • Bart en Mariska's Webstek
Re: Things go bad when not using large strings while in objfpc mode.
« Reply #4 on: June 15, 2025, 11:05:44 pm »
There are no complaints, because the assignement of the event is done in the form, not in code.
You can change the signature of the event (created by the IDE at design time) in the unit, and compilation will succeed.
Below I asked the IDE to create an OnKeyPress event for a TEdit, but then I went ahead and changed the code to:
Code: Pascal  [Select][+][-]
  1.   procedure Edit1KeyPress(Sender: TObject; var Key: String);

This will compiles just fine, but obviously is dead wrong, and it will crash.

The compiler cannot know (AFAIK), the IDE can, and it should warn about this, like Delphi does.

So, this is a known bug/limitation of the Lazarus IDE.

Bart
« Last Edit: June 16, 2025, 02:46:11 pm by Bart »

Thaddy

  • Hero Member
  • *****
  • Posts: 17368
  • Ceterum censeo Trump esse delendam
Re: Things go bad when not using large strings while in objfpc mode.
« Reply #5 on: June 16, 2025, 11:33:24 am »
Oh, well,
Being stupid is an art, unless you were born stupid, like me..
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018