Recent

Author Topic: Loop through list of objects  (Read 1385 times)

Hansvb

  • Sr. Member
  • ****
  • Posts: 397
Loop through list of objects
« 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; 

« Last Edit: July 30, 2021, 05:29:55 pm by Hansvb »
OS Windows 10 - Lazarus 2.0.12

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 985
    • Lebeau Software
Re: Loop through list of objects
« Reply #1 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 set to OwnsObjects=True to manage that for you.
« Last Edit: July 30, 2021, 05:26:37 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Hansvb

  • Sr. Member
  • ****
  • Posts: 397
Re: Loop through list of objects
« Reply #2 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".

OS Windows 10 - Lazarus 2.0.12

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Loop through list of objects
« Reply #3 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018