Recent

Author Topic: IBX 2.7.2, What's wrong with my codes?  (Read 3183 times)

incendio

  • Sr. Member
  • ****
  • Posts: 358
IBX 2.7.2, What's wrong with my codes?
« on: June 18, 2025, 05:14:10 am »
Hi guys,

Just upgrade from Lazarus 2.2.2 to Lazarus 4.0 Windows 32 bit.

Have problem with codes that work well in Lazarus 2.2.2, now not working.

Here are the codes
Code: Pascal  [Select][+][-]
  1. TMainFrm = class(TForm)    
  2. private
  3.    procedure Test(RptId : string;ColHeader : array of string);
  4.    function GetColTitleWidth(RptId : string;Coltitle : string) : integer;
  5. end;
  6.  
  7. function TMainFrm.GetColTitleWidth(RptId : string;Coltitle : string) : integer;
  8. begin
  9.   ShowMessage('From get col : ' + Coltitle);
  10.   DM.Q.SQL.Clear;
  11.   DM.Q.SQL.Add('select wd from m_rpt_col where id_rpt = ' + RptId + ' and title = '''+ Coltitle + '''' );
  12.   DM.Q.Open;
  13.   Result := DM.Q.FieldByName('wd').AsInteger;
  14. end;
  15.  
  16. procedure TMainFrm.Test(RptId : string;ColHeader : array of string);
  17. var
  18.   Col : string;
  19. begin
  20.   Col :=  ColHeader[i];
  21.   ShowMessage(Col);
  22.  
  23.   GetColTitleWidth(RptId,Col);
  24.  

Problem with Lazarus 4 is variable Col in procedure Test has a value, for exam. 'Code', but in function GetColTitleWidth a string append operation with variable not working. ShowMessage('From get col : ' + Coltitle) should be 'From get col : Code', it became 'From get col : '

if code change to
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.Test(RptId : string;ColHeader : array of string);
  2. var
  3.   Col : string;
  4. begin
  5.   Col :=  'Code';
  6.  
  7.   GetColTitleWidth(RptId,Col);
  8.  

It worked OK.

Why is that ?
« Last Edit: June 20, 2025, 11:09:22 am by incendio »

ASerge

  • Hero Member
  • *****
  • Posts: 2434
Re: Lazarus 4, What's wrong with my codes?
« Reply #1 on: June 18, 2025, 06:21:44 am »
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.Test(RptId : string;ColHeader : array of string);
  2. var
  3.   Col : string;
  4. begin
  5.   Col :=  ColHeader[i];
  6.   ShowMessage(Col);
  7.   GetColTitleWidth(RptId,Col);
  8.  
What is variable i?

incendio

  • Sr. Member
  • ****
  • Posts: 358
Re: Lazarus 4, What's wrong with my codes?
« Reply #2 on: June 18, 2025, 06:26:13 am »
Code: Pascal  [Select][+][-]
  1. procedure TMainFrm.Test(RptId : string;ColHeader : array of string);
  2. var
  3.   Col : string;
  4. begin
  5.   Col :=  ColHeader[i];
  6.   ShowMessage(Col);
  7.   GetColTitleWidth(RptId,Col);
  8.  
What is variable i?
Sorry, it was a part of actual code.

i value is 0, type of integer.

440bx

  • Hero Member
  • *****
  • Posts: 5566
Re: Lazarus 4, What's wrong with my codes?
« Reply #3 on: June 18, 2025, 06:27:33 am »
i value is 0, type of integer.
Curious... is it a global variable ?
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

incendio

  • Sr. Member
  • ****
  • Posts: 358
Re: Lazarus 4, What's wrong with my codes?
« Reply #4 on: June 18, 2025, 06:29:26 am »
i value is 0, type of integer.
Curious... is it a global variable ?
No, it is a local variable. Its value from a for loop.

440bx

  • Hero Member
  • *****
  • Posts: 5566
Re: Lazarus 4, What's wrong with my codes?
« Reply #5 on: June 18, 2025, 06:45:15 am »
No, it is a local variable. Its value from a for loop.
if it's a local variable then why isn't it showing as a var in TMainFrm.Test which is where it's used ?
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

Khrys

  • Sr. Member
  • ****
  • Posts: 255
Re: Lazarus 4, What's wrong with my codes?
« Reply #6 on: June 18, 2025, 06:51:53 am »
i value is 0, type of integer.
Curious... is it a global variable ?
No, it is a local variable. Its value from a for loop.

Can you post the full code?

I'm sure the problem's root cause is somewhere in  TMainFrm.Test  or its callers,  but this snippet doesn't contain anything that might break due to version differences.
I suspect the old code already contained a subtle bug (e.g. reliance on a certain manifestation of undefined behavior) that just happened to work until now.

Does  ColHeader  have the right contents upon assignment to  Col? Have you checked with a debugger?

incendio

  • Sr. Member
  • ****
  • Posts: 358
Re: Lazarus 4, What's wrong with my codes?
« Reply #7 on: June 18, 2025, 07:51:51 am »
i value is 0, type of integer.
Curious... is it a global variable ?
No, it is a local variable. Its value from a for loop.

Can you post the full code?

I'm sure the problem's root cause is somewhere in  TMainFrm.Test  or its callers,  but this snippet doesn't contain anything that might break due to version differences.
I suspect the old code already contained a subtle bug (e.g. reliance on a certain manifestation of undefined behavior) that just happened to work until now.

Does  ColHeader  have the right contents upon assignment to  Col? Have you checked with a debugger?
Full code is quite large, beside it won't work either because it needs firebird database to be installed.

Will try to make a sample project to show the problem.
« Last Edit: June 18, 2025, 08:16:39 am by incendio »

incendio

  • Sr. Member
  • ****
  • Posts: 358
Re: Lazarus 4, What's wrong with my codes?
« Reply #8 on: June 18, 2025, 09:13:16 am »
Attached is a complete sample of project.

It won run without firebird database installed, but it has the same problem with string operation.

The error began with this codes
Code: Pascal  [Select][+][-]
  1. function TForm1.CreateArr(RptId: string;NoOfCol : integer) : StrArray;
  2. var
  3.   Header : array of string;
  4.   i : integer;
  5. begin
  6.   i:=0;
  7.   SetLength(Header, NoOfCol);
  8.  
  9.   Q.SQL.Clear;
  10.   Q.SQL.Add('select title from m_rpt_col where ID_RPT = ' + RptId + ' order by id' );
  11.   ShowMessage(Q.SQL.Text);
  12.   Q.Open;
  13.  
  14.   while not(Q.EOF) do
  15.   begin
  16.     Header[i] := Q.FieldByName('title').AsString;
  17.     i := i+1;
  18.     ShowMessage('Header : ' + Header[i]); // Show Header :
  19.     ShowMessage(Q.FieldByName('title').AsString); // Show correctly FieldName
  20.     ShowMessage('Field : ' + Q.FieldByName('title').AsString); // Show Field :
  21.     Q.Next;
  22.   end;
  23.  
  24.   Result := Header;
  25. end;
  26.  
  27.  

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: Lazarus 4, What's wrong with my codes?
« Reply #9 on: June 18, 2025, 09:28:19 am »
The error began with this codes
That's why you always need to post exact code and leave nothing out (unless you have tested it and made sure your shown code also contains the problem).

Look at your line 16 and 17.
You begin with an empty array.
You set Header[0] to a fieldtitle.
Then you increate i and want to display Header[1].
But Header[1] isn't filled yet so it's empty so your line 18 shows an empty text.

Move the i := i + 1 to under Q.Next and it should show the correct texts.

Khrys

  • Sr. Member
  • ****
  • Posts: 255
Re: Lazarus 4, What's wrong with my codes?
« Reply #10 on: June 18, 2025, 09:33:44 am »
Code: Pascal  [Select][+][-]
  1. Q.SQL.Add('select title from m_rpt_col where ID_RPT = ' + RptId + ' order by id' );

What's the type of  ID_RPT? Why isn't  RptId  quoted?
If the query returns no results, then  Header  will contain only empty strings.

(Obligatory: Please use query parameters to save yourself from headaches and SQL injections)


incendio

  • Sr. Member
  • ****
  • Posts: 358
Re: Lazarus 4, What's wrong with my codes?
« Reply #11 on: June 18, 2025, 09:39:31 am »
The error began with this codes
That's why you always need to post exact code and leave nothing out (unless you have tested it and made sure your shown code also contains the problem).

Look at your line 16 and 17.
You begin with an empty array.
You set Header[0] to a fieldtitle.
Then you increate i and want to display Header[1].
But Header[1] isn't filled yet so it's empty so your line 18 shows an empty text.

Move the i := i + 1 to under Q.Next and it should show the correct texts.

You were right, it was an error in this sample, but not in the real project.

Take a look at line 20, showmessage showed empty string for table's record.

incendio

  • Sr. Member
  • ****
  • Posts: 358
Re: Lazarus 4, What's wrong with my codes?
« Reply #12 on: June 18, 2025, 09:42:08 am »
Code: Pascal  [Select][+][-]
  1. Q.SQL.Add('select title from m_rpt_col where ID_RPT = ' + RptId + ' order by id' );

What's the type of  ID_RPT? Why isn't  RptId  quoted?
If the query returns no results, then  Header  will contain only empty strings.

(Obligatory: Please use query parameters to save yourself from headaches and SQL injections)

Type of id_rpt is integer. Query returns with result. Line 19 show the value of the record.

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: Lazarus 4, What's wrong with my codes?
« Reply #13 on: June 18, 2025, 09:45:04 am »
You were right, it was an error in this sample, but not in the real project.

Take a look at line 20, showmessage showed empty string for table's record.
That's why you only paste exact code.

You say line 20 doesn't show Q.FieldByName('title').AsString while the line above does???

Are you absolutely sure this isn't a typo again?

(just asking before trying to create a database and testing the code)



incendio

  • Sr. Member
  • ****
  • Posts: 358
Re: Lazarus 4, What's wrong with my codes?
« Reply #14 on: June 18, 2025, 09:48:50 am »
You were right, it was an error in this sample, but not in the real project.

Take a look at line 20, showmessage showed empty string for table's record.
That's why you only paste exact code.

You say line 20 doesn't show Q.FieldByName('title').AsString while the line above does???

Are you absolutely sure this isn't a typo again?

(just asking before trying to create a database and testing the code)
Yes.

 

TinyPortal © 2005-2018