Loop through list of objects lazarus / 21-General ---------------------------------------------------------------------- Hansvb · 2021-07-30 15:01 Loop through a list wit objects. Hi, I am trying to loop through a list with objects. First i made a class with fields  (TOraConnectionData) Then I fill the objects and add the objects to a object list: 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 while not Query.Eof do    Begin      OraConItem := TOraConnectionData.create;  // Create a new object       OraConItem.ConnectionName := Query.FieldByName('CONNECTNAME').AsString;  // set property      OraConItem.SchemaName := Query.FieldByName('SCHEMANAME').AsString ;              OraConItems.Add(OraConItem);  //Add to the object list. The Object has data!      OraConItem.Free;  //==>> I think this also closes the objectlist. But that is strange.      Query.Next;    end; Untill now everything works. But when i want to get data back from the object list I see the right amount of objects but they seem to be empty. reading data from the objectlist gives empty values: 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 for i:=0 to OraConItems.Count-1 do      // 2 objects = good  begin    OraConItem := OraConItems[i];  // get the first object. this is empty ?        test := OraConItem.ConnectionName; // empty string        //...  end; ---------------------------------------------------------------------- Remy Lebeau · 2021-07-30 15:23 Quote from: Hansvb on July 30, 2021, 05:01:03 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i OraConItems.Add(OraConItem);  //Add to the object list. The Object has data!OraConItem.Free; Why are you destroying the object immediately after adding it to the list?  Your list is full of dangling pointers to invalid memory, that is why you are having trouble accessing the data afterwards.  Don't Free() the objects until a later time when you are done using them.  Consider using a TObjectList set to OwnsObjects=True to manage that for you. ---------------------------------------------------------------------- Hansvb · 2021-07-30 15:39 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 OraConItem.Free;This is the reason i get empty objects. i thought I was freeing a single object but it seems that the list with the objects is freed to. Removing this line is the solution. I don't even get memory leaks which I don't get since I skip "Free". ---------------------------------------------------------------------- lucamar · 2021-07-30 17:43 Quote from: Hansvb on July 30, 2021, 05:39:33 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i OraConItem.Free;This is the reason i get empty objects. i thought I was freeing a single object but it seems that the list with the objects is freed to. Removing this line is the solution. I don't even get memory leaks which I don't get since I skip "Free". You're not freeing the list, it's just that the list stores only a reference to the object, so when you Free the object that reference points to a an object that no longer exits. As for the memory leaks, it's difficult to say without knowing the class of the list and how you're using it but if it's a TObjectList (or descendant) and it has OwnObjects set to True then it takes care of freeing them when you free it, as Remy explained, so there's no memory leaked.