Recent

Author Topic: [SOLVED] Is this intended or bug?  (Read 2870 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1734
[SOLVED] Is this intended or bug?
« on: June 14, 2020, 02:56:28 am »
Hello,
Please see the following example. How can I pass parameter "array of integer" to the dynamic array value of the local variable "aLoop"? 


Code: Pascal  [Select][+][-]
  1. type
  2.    RaLoop = record
  3.       Qno,
  4.       Index : integer;  // index is 1 based
  5.       LoopCodes : array of integer;
  6.    private
  7.       function getLoopTag : string;
  8.       function getLoopStructure : string;
  9.       procedure setLoopStructure (AValue: string);
  10.  
  11.    public
  12.       property LoopTag : string read getLoopTag;
  13.       property LoopStructure : string read getLoopStructure write setLoopStructure;
  14.  
  15.       constructor Create(s: string); overload;
  16.       constructor Create(tqno:integer; codes: array of integer); overload;
  17.       constructor Create(tqno, idx:integer; codes: array of integer); overload;
  18.  
  19.       procedure Free;
  20.    end;
  21.  
  22. // implementation
  23.  
  24. constructor RaLoop.Create(tqno, idx: integer; codes: array of integer);
  25. var
  26.    ti : integer;
  27. begin
  28.    Qno := tqno;
  29.    Index := idx;
  30.  
  31.    SetLength(Self.LoopCodes, Length(Codes));
  32.    for ti := 0 to High(Codes) do LoopCodes[ti] := Codes[ti];
  33.    ShowMessage(IntToStr(Length(LoopCodes)));    //--------------- This shows 3
  34. end;
  35.  
  36. procedure TForm1.Button1Click(Sender: TObject);
  37. var
  38.    aLoop: RaLoop;
  39. begin
  40.    aLoop.Create(100, 2, [3,6,9]);
  41.    ShowMessage(IntToStr(Length(aLoop.LoopCodes)));  //----------  this displays 0
  42. end;
  43.  
« Last Edit: June 15, 2020, 05:07:55 am by egsuh »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Is this intended or bug?
« Reply #1 on: June 14, 2020, 03:20:47 am »
You call constructor wrongly. Use:
Code: Pascal  [Select][+][-]
  1. aLoop:=RaLoop.Create(100, 2, [3,6,9]);
Constructor is in fact a class function whose result is a pointer to a new instance of that class.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

jamie

  • Hero Member
  • *****
  • Posts: 7492
Re: Is this intended or bug?
« Reply #2 on: June 14, 2020, 12:26:14 pm »
The actual problem is not so much as a pointer being returned because that isn't really what is happening, the whole object is copied over from the stack if you do a local test..

 So initially a background copy is created on the stack, executed and a copy is stored in the resulting variable is that is how it's being used at the time.. Because there is no memory leaks otherwise...
 
 The issue is the use of the managed Array that is being used. the system is cleaning it up on exit if not being made a copy of first..
 
 Just one of those Managed things I guess..

 Because if you use a non-managed array its still living when exiting the constructor.

The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 6272
  • Compiler Developer
Re: Is this intended or bug?
« Reply #3 on: June 14, 2020, 01:13:25 pm »
The actual problem is not so much as a pointer being returned because that isn't really what is happening, the whole object is copied over from the stack if you do a local test..

No, Blaazen is right. The problem is the use of the constructor as a method instead of a constructor.

So initially a background copy is created on the stack, executed and a copy is stored in the resulting variable is that is how it's being used at the time.. Because there is no memory leaks otherwise...
 
 The issue is the use of the managed Array that is being used. the system is cleaning it up on exit if not being made a copy of first..
 
 Just one of those Managed things I guess..

 Because if you use a non-managed array its still living when exiting the constructor.

This has nothing to do with a managed array, the problem is that the RaLoop constructor returns the Self, but it isn't captured, instead the local aLoop variable stays uninitialized.

jamie

  • Hero Member
  • *****
  • Posts: 7492
Re: Is this intended or bug?
« Reply #4 on: June 14, 2020, 04:56:44 pm »
really ?

I tested that without using a managed array and it works fine..

I also looked a the assembly code..

So what ever, ,I know nothing...

Have a good day..

The only true wisdom is knowing you know nothing

egsuh

  • Hero Member
  • *****
  • Posts: 1734
Re: Is this intended or bug?
« Reply #5 on: June 15, 2020, 05:07:40 am »
Hahaha...

Blaazen was right. I should have written

      aLoop := RaLoop.Create(100, 2, [3,6,9]);

instead of

       aLoop.Create(100, 2, [3,6,9]);


I made similar mistakes several times (with class, not records), and still am making the same mistakes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6272
  • Compiler Developer
Re: Is this intended or bug?
« Reply #6 on: June 15, 2020, 09:11:13 am »
Hahaha...

Blaazen was right. I should have written

      aLoop := RaLoop.Create(100, 2, [3,6,9]);

instead of

       aLoop.Create(100, 2, [3,6,9]);


I made similar mistakes several times (with class, not records), and still am making the same mistakes.

Though in all fairness, the second one should work as well, as in Delphi it works without problems. It might be due to the way record constructors are implemented in FPC. I'll probably need to change that anyway so that we can support them in New as well.

egsuh

  • Hero Member
  • *****
  • Posts: 1734
Re: [SOLVED] Is this intended or bug?
« Reply #7 on: June 15, 2020, 09:16:47 am »
Quote
Though in all fairness, the second one should work as well, as in Delphi it works without problems. It might be due to the way record constructors are implemented in FPC. I'll probably need to change that anyway so that we can support them in New as well.

Well if it is done, that would be much better. Anyway constructor of record is not allocating new memory block as class.

 

TinyPortal © 2005-2018