TFPSMap.IndexOfData function issue lazarus / 21-General ---------------------------------------------------------------------- zoltanleo · 2022-11-11 22:14 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  ...fgl...; type  TMyTest = specialize TFPGMap;...  public    property MyTest: TMyTest read FMyTest write SetMyTest;  end;...var  i: PtrInt = 0;begin  FMyTest:= TMyTest.Create;    for i:= 0 to 9 do  MyTest.AddOrSetData(i,Format('string_%d (added as Object: %d)',[Succ(i),i]));;   ComboBox1.Clear;   for i:= 0 to Pred(MyTest.Count) do    ComboBox1.Items.Add(MyTest.Data[i]);   ComboBox1.ItemIndex:= 0;  ComboBox1Change(Sender);... procedure TForm1.ComboBox1Change(Sender: TObject);var  k: PtrInt = 0;begin  Label1.Caption:= 'no matches';   k:= MyTest.IndexOfData(ComboBox1.Items[ComboBox1.ItemIndex]);  if (k > -1) then    Label1.Caption:= 'Key: ' + IntToStr(MyTest.Keys[k]);   Caption:= IntToStr(MyTest.Count);end; Plz someone confirm my guess ---------------------------------------------------------------------- marcov · 2022-11-11 22:18 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 · 2022-11-12 17:16 Hi marcov Thank U for answer ---------------------------------------------------------------------- PascalDragon · 2022-11-13 14:59 Quote from: zoltanleo on November 11, 2022, 11:14:21 pmFor 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { snip }uses  StrUtils; {$R *.lfm} { TForm1 } function CompareStringFunc(const aData1, aData2: String): Integer;begin  Result := AnsiCompareStr(aData1, aData2);end; procedure TForm1.FormCreate(Sender: TObject);var  i: PtrInt = 0;begin  FMyTest:= TMyTest.Create;  FMyTest.OnDataCompare := @CompareStringFunc;{snip} Two additional points: the corresponding counterpart to the OnCreate event is the OnDestroy event; OnClose might be called multiple timesyour setter for MyTest does not free the original list thus you'll leak memory with every use Quote from: marcov on November 11, 2022, 11:18:08 pmThere 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 · 2022-11-13 17:41 Added this new info to https://wiki.freepascal.org/TFPGMap