Recent

Author Topic: [Solved] TThreadList Free objects?  (Read 1633 times)

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
[Solved] TThreadList Free objects?
« on: November 12, 2020, 04:51:38 pm »
Hi, I'm manipulating a list of objects in multiple threads, one for adding and other for iteration / removing items.

I found TThreadList, and seems ok, but I have a doubt about freeing the objects, this way is ok?

I've implemented only in the destructor, but I need as well in the Remove item method, if is ok in the destructor I will do the same there.

Thanks.

Code: Pascal  [Select][+][-]
  1.    TEstadoMercadoPagoItem = (empPendiente, empAprobado, empVisto);
  2.  
  3.    { TMercadoPagoItem }
  4.  
  5.    TMercadoPagoItem = class(TObject)
  6.    public
  7.      iMesa: Integer;
  8.      rTotal: Real;
  9.      sReferencia: String;
  10.      sEstado: TEstadoMercadoPagoItem;
  11.      constructor Create(AMesa: Integer; ATotal: Real; AReferencia: String);
  12.    end;
  13.  
  14.    { TMercadoPagoList }
  15.  
  16. TMercadoPagoList = class(TThreadList)
  17.      destructor Destroy; override;
  18.    end;
  19.  
  20. var
  21.   MercadoPagoList: TMercadoPagoList;
  22.  
  23.   implementation
  24.  
  25.   { TMercadoPagoList }
  26.  
  27.     destructor TMercadoPagoList.Destroy;
  28.     var
  29.       i: integer;
  30.       list: TList;
  31.       p: TPointer;
  32. begin
  33.   list := LockList;
  34.   try
  35.     for i:=list.Count-1 downto 0 do
  36.     begin
  37.       p := list.Items[i];
  38.       list.Remove(p);
  39.       TMercadoPagoItem(p).Free;
  40.     end;
  41.   finally
  42.     UnlockList;
  43.   end;
  44.   inherited Destroy;
  45. end;
« Last Edit: November 12, 2020, 09:27:54 pm by lainz »

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: TThreadList Free objects?
« Reply #1 on: November 12, 2020, 05:06:35 pm »
Or if that is not as good, is possible to use records with TThreadList? Since my object just holds data, there are no internal methods involved.. Thanks.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: TThreadList Free objects?
« Reply #2 on: November 12, 2020, 05:43:25 pm »
TThreadList uses an internal TList which is returned by LockList so you can add an observer to it and free whatever needs to be freed in the notification handler.

However, if you're sure you'll never use that feature you can (and probably should) either override the default Remove method or sub-class TThreadList to add a new RemoveAndFree() method to take care of that.

HTH!

ETA: And yes, you can use almost anything: records, classes, simple types, ... As TList, TThreadList deals only with pointers so you can use whatever you can get a pointer from. ;)
« Last Edit: November 12, 2020, 05:46:10 pm by lucamar »
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.

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: TThreadList Free objects?
« Reply #3 on: November 12, 2020, 09:27:45 pm »
Thanks, so I will use records.

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: [Solved] TThreadList Free objects?
« Reply #4 on: November 13, 2020, 07:07:31 pm »
I had to use objects because records didn't work.
I have to edit the content once added...

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [Solved] TThreadList Free objects?
« Reply #5 on: November 13, 2020, 07:47:14 pm »
There should be no problem, only with records you have to remember to dereference the pointer, because it's not automagically done like with (new-style) objects, so something like:

Code: Pascal  [Select][+][-]
  1.   p := list.Items[i];
  2.   p^.sEstado := emAprobado;
  3.   { Or just ... }
  4.   list.Items[i]^.rTotal := 300.50;

Of course, nothing inherently "wrong" in doing it with objects/classes; only it's somewhat "wasteful" if a simple (or even an advanced) record would suffice.
« Last Edit: November 13, 2020, 07:50:52 pm by lucamar »
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.

lainz

  • Hero Member
  • *****
  • Posts: 4740
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: [Solved] TThreadList Free objects?
« Reply #6 on: November 13, 2020, 09:08:02 pm »
There should be no problem, only with records you have to remember to dereference the pointer, because it's not automagically done like with (new-style) objects, so something like:

Code: Pascal  [Select][+][-]
  1.   p := list.Items[i];
  2.   p^.sEstado := emAprobado;
  3.   { Or just ... }
  4.   list.Items[i]^.rTotal := 300.50;

Of course, nothing inherently "wrong" in doing it with objects/classes; only it's somewhat "wasteful" if a simple (or even an advanced) record would suffice.

Thanks, yes maybe that was the problem, I never did programming with pointers and records.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [Solved] TThreadList Free objects?
« Reply #7 on: November 14, 2020, 08:01:56 am »
Thanks, yes maybe that was the problem, I never did programming with pointers and records.

It's not very difficult but it requires a little more attention to details such as when you're dealing with the pointer and when with the thing it points to, where is that thing: local/stack or heap, whether you need to reserve memory or is reserved by a declaration, etc.

Funny things like those :)
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