Lazarus

Programming => General => Topic started by: Hansvb on July 30, 2021, 05:01:03 pm

Title: Loop through list of objects
Post by: Hansvb on July 30, 2021, 05:01:03 pm
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][+][-]
  1. while not Query.Eof do
  2.     Begin
  3.       OraConItem := TOraConnectionData.create;  // Create a new object
  4.  
  5.       OraConItem.ConnectionName := Query.FieldByName('CONNECTNAME').AsString;  // set property
  6.       OraConItem.SchemaName := Query.FieldByName('SCHEMANAME').AsString ;        
  7.  
  8.       OraConItems.Add(OraConItem);  //Add to the object list. The Object has data!
  9.       OraConItem.Free;  //==>> I think this also closes the objectlist. But that is strange.
  10.       Query.Next;
  11.     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][+][-]
  1. for i:=0 to OraConItems.Count-1 do      // 2 objects = good
  2.   begin
  3.     OraConItem := OraConItems[i];  // get the first object. this is empty ?
  4.         test := OraConItem.ConnectionName; // empty string
  5.         //...
  6.   end; 

Title: Re: Loop through list of objects
Post by: Remy Lebeau on July 30, 2021, 05:23:46 pm
Code: Pascal  [Select][+][-]
  1. OraConItems.Add(OraConItem);  //Add to the object list. The Object has data!
  2. 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 (https://www.freepascal.org/docs-html/current/fcl/contnrs/tobjectlist.html) set to OwnsObjects=True to manage that for you.
Title: Re: Loop through list of objects
Post by: Hansvb on July 30, 2021, 05:39:33 pm
Code: Pascal  [Select][+][-]
  1. 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".

Title: Re: Loop through list of objects
Post by: lucamar on July 30, 2021, 07:43:36 pm
Code: Pascal  [Select][+][-]
  1. 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.
TinyPortal © 2005-2018