Board index » delphi » TStringList vs. Array of TStringList Confusion

TStringList vs. Array of TStringList Confusion

In article <3476b8bd.4416...@news.mindspring.com> smi...@removethis.imagesoftcorp.com (Stephen Smith) writes:

Quote
>Can someone explain something to me?  Why is it that if I define a
>variable as a TStringList, I have to explicitly create it, but if I
>define a variable as an array of TStringList, I don't?

"An object is a pointer."  "An object is a pointer."  "An object is a pointer."

They removed the silly "^"s but otherwise that's all they did.  So when you
define a TStringList object you are defining a 4-byte, probably-initially-nil,
pointer variable; nothing more.  When you define an "array of TStringList,"
you are defining an array of, probably-initially-nil (but don't count on it)
pointer variables.

Quote
>Also, if I have an array of TStringLists, do I need to explicitly free
>each TStringList element myself?  Do I need to free anything?

Contrary to Borland's documentation, removing an element from a list does
*not* affect any object to which the list referred.  But it's a snap to define
a "TTidyStringList" object-class which *does* automagically remove the objects
-- when you want to do that.
 

Re:TStringList vs. Array of TStringList Confusion


Can someone explain something to me?  Why is it that if I define a
variable as a TStringList, I have to explicitly create it, but if I
define a variable as an array of TStringList, I don't?

Also, if I have an array of TStringLists, do I need to explicitly free
each TStringList element myself?  Do I need to free anything?

Thanks,
Steve

Re:TStringList vs. Array of TStringList Confusion


Quote
In article <3476b8bd.4416...@news.mindspring.com>, Stephen Smith wrote:
> but if I define a variable as an array of TStringList,
> I don't?

The compiler automatically allocates memory for simple types, such as
integer, string, float, records, arrays, etc. The programmer needs to
allocate memory for class types, such as TStringList, by using a
constructor such as the Create method.

While you didn't need to issue a Create method on an _array_ of
TStringList instances, you will need to issue a Create method on each
_array element_, or else you receive an Access Violation. In other
words, if your array contains objects, the memory for the array is
automatically allocated, but you'll need to use Create to instantiate
the object instances stored in the array.

Quote
> Also, if I have an array of TStringLists, do I need to
> explicitly free each TStringList element myself?

Absolutely. Any time you create a non-component object, you must free
it. TStringList descends from TPersistent, not TComponent, so it is a
non-component object which you must free.

--
Rick Rogers (TeamB) | Fenestra Technologies

Re:TStringList vs. Array of TStringList Confusion


Quote
In article <3476b8bd.4416...@news.mindspring.com>, smi...@removethis.imagesoftcorp.com wrote:
>Can someone explain something to me?  Why is it that if I define a
>variable as a TStringList, I have to explicitly create it, but if I
>define a variable as an array of TStringList, I don't?

>Also, if I have an array of TStringLists, do I need to explicitly free
>each TStringList element myself?  Do I need to free anything?

>Thanks,
>Steve

Well, you do have to explicitly Create (and Free) each *member* of the array.

So:

procedure DoSomething;
  var
  AS:  array[1..5] of TStringList;
  I: integer;

  begin
  // Do this first so that the try..finally will always work.
  for I := 1 to 5 do
    AS[I] := nil;
  try
    for I := 1 to 5 do
      AS[I] := TStringList.Create;
    // Do whatever
    ...
  finally
    for I := 1 to 5 do
      AS[I].Free;
  end; // finally
  end; // DoSomething

The array itself doesn't need to be Created or Free'd, since it's, well, just
an array.  But the individual members have to be handled in the same way you'd
normally handle a Delphi object.

Geo

--
George Mealer                       geo...@cambria.com
Programmer/Systems Analyst          Office: (650) 328-9270
Cambria Corporation                 Fax:    (650) 328-8642

"A good programmer is someone who looks both ways before
 crossing a one-way street." -- Doug Linder

Re:TStringList vs. Array of TStringList Confusion


Quote
Stephen Smith wrote:

> Can someone explain something to me?  Why is it that if I define a
> variable as a TStringList, I have to explicitly create it, but if I
> define a variable as an array of TStringList, I don't?

        Do you actually mean an array of TStringList, as in

var s: array[0..10] of TStringList;

? Seems like this came up once before and nobody believed that was
what the guy (you?) meant.

        If you mean something else then you need to say what. Assumimg
you actually do mean an array of TStringList: You don't need to Create
it because it's not an object - it's an array. You DO need to Create
the stringlists in the array before using them:

for j:=0 to 10 do
 s[j]:= TStringList.Create;

That's because s[j] is an object.

Quote
> Also, if I have an array of TStringLists, do I need to explicitly free
> each TStringList element myself?  Do I need to free anything?

        You need to Free any TStringlists that you've Created, unless
you want to leak memory. As in

for j:=0 to 10 do
 s[j].Free;

--
David Ullrich

sig.txt not found

Other Threads