* * *

Author Topic: Tlist problem  (Read 245 times)

lazer

  • New member
  • *
  • Posts: 25
Tlist problem
« on: October 07, 2017, 12:38:33 pm »
Hi,

I have done quite a lot of TP and Delphi, but it's was a long time ago and I'm having trouble using a Tlist.

I want to use it to store a list of integers. Simple enough.

I added a Tlist to the form , thinking that this would get it an instance of Tlist.
Code: Pascal  [Select]
  1.  private
  2.     { private declarations }
  3.   public
  4.     testx:integer;
  5.     digits:Tlist;
  6.  

However, when I try to use it in FormCreate() it is null and causes an exception.
Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.    digits.clear;
  4. end;
  5.  
  6.  

What am I missing?

thanks.

GetMem

  • Hero Member
  • *****
  • Posts: 2468
Re: Tlist problem
« Reply #1 on: October 07, 2017, 12:41:29 pm »
Hi lazer,

You have to create the list and make sure you free it on form destroy, to prevent memory leaks:
Code: Pascal  [Select]
  1. //...
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. begin
  5.   Digits := TList.Create;
  6. end;
  7.  
  8. procedure TForm1.FormDestroy(Sender: TObject);
  9. begin
  10.   Digits.Free;
  11. end;

Handoko

  • Hero Member
  • *****
  • Posts: 1801
  • My goal: build my own game engine using Lazarus
Re: Tlist problem
« Reply #2 on: October 07, 2017, 01:03:16 pm »
The TList documentation:
https://www.freepascal.org/docs-html/rtl/classes/tlist.html

Anyone looking for tutorial for TList (suitable for newbies), you may start reading from here:
http://forum.lazarus.freepascal.org/index.php/topic,38136.msg260867.html#msg260867

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus