Recent

Author Topic: TFPSMap.IndexOfData function issue  (Read 972 times)

zoltanleo

  • Hero Member
  • *****
  • Posts: 509
TFPSMap.IndexOfData function issue
« on: November 11, 2022, 11:14:21 pm »
Hi folks.

For some reason I am trying to use fgl.TFPGMap instead of generics.collection.TDictionary. It seemed to me that the TFPSMap.IndexOfData function was not working properly. Here is an example code that reproduces the issue.
Code: Pascal  [Select][+][-]
  1. uses
  2.   ...fgl...;
  3.  
  4. type
  5.   TMyTest = specialize TFPGMap<PtrInt,String>;
  6. ...
  7.   public
  8.     property MyTest: TMyTest read FMyTest write SetMyTest;
  9.   end;
  10. ...
  11. var
  12.   i: PtrInt = 0;
  13. begin
  14.   FMyTest:= TMyTest.Create;
  15.  
  16.  
  17.   for i:= 0 to 9 do
  18.   MyTest.AddOrSetData(i,Format('string_%d (added as Object: %d)',[Succ(i),i]));;
  19.  
  20.   ComboBox1.Clear;
  21.  
  22.   for i:= 0 to Pred(MyTest.Count) do
  23.     ComboBox1.Items.Add(MyTest.Data[i]);
  24.  
  25.   ComboBox1.ItemIndex:= 0;
  26.   ComboBox1Change(Sender);
  27. ...
  28.  
  29. procedure TForm1.ComboBox1Change(Sender: TObject);
  30. var
  31.   k: PtrInt = 0;
  32. begin
  33.   Label1.Caption:= 'no matches';
  34.  
  35.   k:= MyTest.IndexOfData(ComboBox1.Items[ComboBox1.ItemIndex]);
  36.   if (k > -1) then
  37.     Label1.Caption:= 'Key: ' + IntToStr(MyTest.Keys[k]);
  38.  
  39.   Caption:= IntToStr(MyTest.Count);
  40. end;

Plz someone confirm my guess
Win10 LTSC x64/Deb 12 amd64(gtk2)/Kubuntu(qt5)/Darwin Cocoa x86_64 (Sequoia):
Lazarus x32_64 (trunk); FPC(trunk), FireBird 3.0.11; IBX by TonyW

Sorry for my bad English, I'm using translator ;)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12571
  • FPC developer.
Re: TFPSMap.IndexOfData function issue
« Reply #1 on: November 11, 2022, 11:18:08 pm »
There are known issues with fgl and ref counted types e.g.

https://gitlab.com/freepascal.org/fpc/source/-/issues/39846

If they are clear to fix, I usually fix them, but in this case I have no idea what the original author meant to do.

I suggest to use TDictionary.

zoltanleo

  • Hero Member
  • *****
  • Posts: 509
Re: TFPSMap.IndexOfData function issue
« Reply #2 on: November 12, 2022, 06:16:41 pm »
Hi marcov

Thank U for answer
Win10 LTSC x64/Deb 12 amd64(gtk2)/Kubuntu(qt5)/Darwin Cocoa x86_64 (Sequoia):
Lazarus x32_64 (trunk); FPC(trunk), FireBird 3.0.11; IBX by TonyW

Sorry for my bad English, I'm using translator ;)

PascalDragon

  • Hero Member
  • *****
  • Posts: 6238
  • Compiler Developer
Re: TFPSMap.IndexOfData function issue
« Reply #3 on: November 13, 2022, 03:59:01 pm »
For some reason I am trying to use fgl.TFPGMap instead of generics.collection.TDictionary. It seemed to me that the TFPSMap.IndexOfData function was not working properly. Here is an example code that reproduces the issue.

By default TFPGMap<> does not provide a comparison for the Data type. You need to provide it by assigning the OnDataCompare event:

Code: Pascal  [Select][+][-]
  1. { snip }
  2. uses
  3.   StrUtils;
  4.  
  5. {$R *.lfm}
  6.  
  7. { TForm1 }
  8.  
  9. function CompareStringFunc(const aData1, aData2: String): Integer;
  10. begin
  11.   Result := AnsiCompareStr(aData1, aData2);
  12. end;
  13.  
  14. procedure TForm1.FormCreate(Sender: TObject);
  15. var
  16.   i: PtrInt = 0;
  17. begin
  18.   FMyTest:= TMyTest.Create;
  19.   FMyTest.OnDataCompare := @CompareStringFunc;
  20. {snip}

Two additional points:
  • the corresponding counterpart to the OnCreate event is the OnDestroy event; OnClose might be called multiple times
  • your setter for MyTest does not free the original list thus you'll leak memory with every use

There are known issues with fgl and ref counted types e.g.

https://gitlab.com/freepascal.org/fpc/source/-/issues/39846

If they are clear to fix, I usually fix them, but in this case I have no idea what the original author meant to do.

I suggest to use TDictionary.

This has nothing to do with reference counted types.

AlexTP

  • Hero Member
  • *****
  • Posts: 2654
    • UVviewsoft
Re: TFPSMap.IndexOfData function issue
« Reply #4 on: November 13, 2022, 06:41:39 pm »

 

TinyPortal © 2005-2018