Link to home
Start Free TrialLog in
Avatar of PiersBull
PiersBull

asked on

Creating TStringLists

Hi,
    I'm trying to dynamically create a number of TStringLists stored in an array. When I try to do this, I get an EGPFault. I don't know why.
The array is declared :

columns : array[1..5] of TStringList;

They are created thus :

for z := 1 to cols do
         columns[z].Create;

where cols is just an int between 1 and 10.
It gives the error when z is 1, so I'm stumped.
Can anyone help?

Cheers, Nik
Avatar of bijoyn
bijoyn

Which version of Delphi are u using ?.
Avatar of PiersBull

ASKER

Hi bijoyn,
   I'm on Delphi 1.
Cheers

ASKER CERTIFIED SOLUTION
Avatar of inter
inter
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried your e.g. in Delphi 2 & 3 and it works absolutely fine. All I can say is change your 'For' loop to 'For z := 0 to 5' and try the same thing. I know this sounds stupid but I cannot think of anything else.
Inter is absolutely right, How stupid can I get. Sorry Nik. Thanks Inter.
Hi again,
    That seems to have sorted the problem out, but now I can't free the memory! I've tried this :

for z := 0 to 5 do
columns[z] := TStringList.Free;

This gives an invalid method error. Any ideas?

Cheers, Nik

Hi,
just try this code :

for z := 0 to 5 do
   columns[z].Free;

Now colums is instanciated and contains an instance, not a class pointer.

Slash/d003303
Hi,
as d003303 said, you should free the instance. Actually (in general) you only need to make a call such as TStringList.Create in creation.        IGOR
Cheers, guys!