Freeing TFPGMap lazarus / 62-General ---------------------------------------------------------------------- egsuh · 2022-02-14 02:46 Hello, If I define as follows, 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 type    TMyclass = class            ....    end;     TVarList = TFPGMap;  var    MyClass: TMyClass;     VarList: TVarList;     SomeStr : string;  begin    VarList := TVarList.Create;   for a while // this is not correct code, of course        MyClass := TMyclass.Create;         VarList.Add(SomeStr, MyClass);    end;     VarList.Free; In this case, does VarList.Free free all instances of MyClass? If I do not want to free them, what list container should I use? ---------------------------------------------------------------------- Thaddy · 2022-02-14 07:33 You can set the property FreeObjects to true or false as long it is an object container. Anyway, use one of the TfpgObjectxxxList descendants otherwise. Or TObjectDictionary<> from generics.collections where you can set ownership in the constructor. (Note map is the same as dictionary) ---------------------------------------------------------------------- egsuh · 2022-02-14 07:48 I have tested this --- objects are not freed by TFPGMap. ---------------------------------------------------------------------- Thaddy · 2022-02-14 07:53 Quote from: egsuh on February 14, 2022, 08:48:07 amI have tested this --- objects are not freed by TFPGMap. As I wrote, it needs an object capable descendant, which is a constraint, otherwise it has no capability to free objects. The object dictionary from generics.collections has that capability. Will add a small example later if necessary. It is   TObjectDictionary that will work for you. Ownership can be set in one of the constructors. TValue has a constraint to classes here. (through its inheritance, the code is difficult, but very good and easy to use) EDIT E.g: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 {$mode delphi}uses generics.collections;type TOwnerDictionary = TObjectDictionary;var   t: TOwnerDictionary;   i:integer;begin   t:= TOwnerDictionary.create([doOwnsValues]); //parameter is a set of doOwnsKeys, doOwnsValues   try     for i := 0 to 9 do       t.add(i,Tobject.create);  // create some objects   finally     t.free; // free all objects too.   end;end.     You should be able to achieve what you want with this example. ---------------------------------------------------------------------- Thaddy · 2022-02-14 08:24 The same goes for fgl: TFPGMapObject has a constructor that sets ownership. 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 {$mode delphi}uses fgl;type TOwnerDictionary = TFPGMapObject;var   t: TOwnerDictionary;   i:integer;begin   t:= TOwnerDictionary.create(true); // true determines ownership   try     for i := 0 to 9 do       t.add(i,Tobject.create);   finally     t.free;   end;end.     That should be enough for today  (Note I came up with these examples by just reading the sourcecode. Highly recommended practice..) ---------------------------------------------------------------------- egsuh · 2022-02-14 08:33 Ah... there was ownership. Thank you, Thaddy. ---------------------------------------------------------------------- PascalDragon · 2022-02-14 14:40 Quote from: Thaddy on February 14, 2022, 08:33:09 amYou can set the property FreeObjects to true or false as long it is an object container. TFPGMap<> (or TFPGList<>) does not have a OwnsObjects or similar property (or parameter of the constructor). That's why the TFPGMapObject<,> or TFPGObjectList<> types exist after all. ---------------------------------------------------------------------- Thaddy · 2022-02-14 17:22 @Sarah, That is nitpicking and I explained it. Come and have a winter BBQ... So we can roast each other. (Serious invite, btw) ---------------------------------------------------------------------- PascalDragon · 2022-02-15 08:59 Quote from: Thaddy on February 14, 2022, 06:22:59 pmThat is nitpicking and I explained it. egsuh use a TFPGMap<> in their code and the first sentence of your response is: Quote from: Thaddy on February 14, 2022, 08:33:09 amYou can set the property FreeObjects to true or false as long it is an object container. That is simply wrong, cause that type does not have that property. Thus this is no nitpicking, but avoids unnecessary confusion: I can very well imagine egsuh or some other user that reads this thread to ask back why there is no FreeObjects property in TFPGMap<> when you said otherwise. While you suggest to use the descendants for use with class instance in the next sentence, you don't do it in a way that corrects the first sentence. ---------------------------------------------------------------------- Thaddy · 2022-02-15 09:54 I pointed out that that he should use a class that supports objects, did't I? ---------------------------------------------------------------------- PascalDragon · 2022-02-15 12:32 But not in the sense of “, but to use/have that property you need to use TFPGMapObject<,> instead” or something like that. If someone reads only the first sentence of your response and tries to apply that suggestion they'll notice that your suggestion was simply wrong. And if one includes your second sentence which contains that suggestion it doesn't imply that the second is necessary for the first. You're merely suggesting “you should use TFPGMapObject<,>, etc. anyway”. In no way does that suggest that this is necessary to use OwnsObjects, because you've phrased it as a general suggestion.