Recent

Author Topic: generics (fgl): property doesn't work correctly  (Read 142 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 935
generics (fgl): property doesn't work correctly
« on: September 11, 2019, 01:54:36 pm »
I'm calling my own procedure clear() when I want to use a (generic) objectlist again.
Code: Pascal  [Select]
  1.     procedure TMain.ClearAll;
  2.     var index : integer;
  3.         rij   : TExpedRij;
  4.     begin
  5.       for index := fBlad.ExpeditieRijen.Count - 1 downto 0 do
  6.       begin
  7.         rij := fBlad.ExpeditieRijen[index];
  8.         rij.Free;
  9.       end;
  10.       fBlad.ExpeditieRijen.Clear;
  11.     end;
  12.  
You have to clean up all inserted objects. Then I call property clear to set the index to 0.
First time goes well. Second time creates a sigdev.
But why? Cleaning up all the objects and set the index to 0 is very logical.
This is the code in FPC:
Code: Pascal  [Select]
  1.     procedure TFPSList.Clear;
  2.     begin
  3.       if Assigned(FList) then
  4.       begin
  5.         SetCount(0);
  6.         SetCapacity(0);
  7.       end;
  8.     end;
  9.  
Very straight line. But why a sigdev. For testing I put the line 'fBlad.ExpeditieRijen.Clear;' before the for loop. no sigdev, but the for loop  will not executed at all.
Code: Pascal  [Select]
  1.     procedure TMain.ClearAll;
  2.     var index : integer;
  3.         rij   : TExpedRij;
  4.     begin
  5.       fBlad.ExpeditieRijen.Clear;
  6.       for index := fBlad.ExpeditieRijen.Count - 1 downto 0 do
  7.       begin
  8.         rij := fBlad.ExpeditieRijen[index];
  9.         rij.Free;
  10.       end;
  11.     end;
  12.  
People always told my to clean up you own objects.

Does property clear clears all object automatically?

By the way function clear is calling a procedure twice.
Code: Pascal  [Select]
  1.     procedure TFPSList.SetCount(NewCount: Integer);
  2.     begin
  3.       if (NewCount < 0) or (NewCount > MaxListSize) then
  4.         Error(SListCountError, NewCount);
  5.       if NewCount > FCapacity then
  6.         SetCapacity(NewCount);
  7.       if NewCount > FCount then
  8.         FillByte(InternalItems[FCount]^, (NewCount-FCount) * FItemSize, 0)
  9.       else if NewCount < FCount then
  10.         Deref(NewCount, FCount-1);
  11.       FCount := NewCount;
  12.     end;
  13.  
Lazarus 1.84 (32b) / FPC 3.0.4
Windows 10

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7362
Re: generics (fgl): property doesn't work correctly
« Reply #1 on: September 11, 2019, 02:00:47 pm »
How is the container declared and instantiated ? Try to make a small console program that mimics the problem, that is easier to communicate over.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5575
    • wiki
Re: generics (fgl): property doesn't work correctly
« Reply #2 on: September 11, 2019, 02:31:29 pm »
objectlist

An objectlist may have on Own(s)Objects, and if it has and if it is set to true, then such an object list would call destroy on the objects.

As good practise your code could nil the objcets
Code: Pascal  [Select]
  1.         rij := fBlad.ExpeditieRijen[index];
  2.         rij.Free;
  3.         fBlad.ExpeditieRijen[index] := nil;

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7362
Re: generics (fgl): property doesn't work correctly
« Reply #3 on: September 11, 2019, 02:37:05 pm »
fgl.tfpgobjectlist seems to have a freeobjects, which is a default property of CREATE set to true

    constructor Create(FreeObjects: Boolean = True);