Board index » delphi » question about class instances and TList

question about class instances and TList

Hi everyone,

I'm trying to develop a small program in Delphi 2.0, as an hobby, but
I'm just a beginer in this object-oriented things, so I ended up
confused with a very simple (I think) question. I thought perhaps you
could help me:

Let's suppose I have a class TMyClass, and it has a field MyField. Then
I declare a variable, say, 'MyVar', for example. MyVar:=TMyClass.Create;
As far as I understand it, this will create an instance of TMyClass
somewhere in the heap, and assign a pointer to it.
I also have a MyList : TList, and I have already constructed it:
MyList:=TList.Create;
Then, I add MyVar to the list: MyList.Add(MyVar); Ok, so far so good.

I want to add a couple more of MyClass instances to MyList. So, I tried
to keep reusing the MyVar variable in a loop, something like this:

begin
MyVar:=TMyClass.Create;
MyVar.MyField:=ReadValue(FromGodKnowsWhere);
MyList.Add(MyVar);
end

...and it worked out. All the values where correct.

My question is: TList is just a list of pointers, right? It holds no
data, just a list of pointers to the adress where each item is, right?
So, each item in MyList is actually just a pointer to an instance of
MyVar. But I've been using the same variable over and over. I know that
by some magical arts each time I use the MyVar:=TMyClass.Create I
actually create another instance of the class, that will be sent to
another place in the heap. BUT, how can I clean all that garbage when I
want to free the memory? I mean, I can use MyList.Free : this will free
the memory where the pointers to the real data are stored. I can
MyVar.Free, and this will free the memory used by the class instance
that was assigned to MyVar (that is, the last one to be created). But
what about the other previous instances of MyClass?

I would really appreciate if someone could tell me how this works.
Please forgive me if this sounds very stupid or if I'm missing something
obvious, but this is really intriguing me...

And if, as I believe, I'm just lucky that this worked out, because this
construction should crash the program at the first place, what would be
the correct way to do this?

Thanks for your time,
Pedro Figueiredo

 

Re:question about class instances and TList


Hi Pedro.

Figueiredo <np7...@mail.telepac.pt> skrev i en
nyhedsmeddelelse:37977233.97215...@mail.telepac.pt...

Quote
> Hi everyone,

> Let's suppose I have a class TMyClass, and it has a field MyField. Then

MyField is normally called a property.

Quote
> I declare a variable, say, 'MyVar', for example. MyVar:=TMyClass.Create;
> As far as I understand it, this will create an instance of TMyClass
> somewhere in the heap, and assign a pointer to it.
> I also have a MyList : TList, and I have already constructed it:
> MyList:=TList.Create;
> Then, I add MyVar to the list: MyList.Add(MyVar); Ok, so far so good.

> I want to add a couple more of MyClass instances to MyList. So, I tried
> to keep reusing the MyVar variable in a loop, something like this:

> begin
> MyVar:=TMyClass.Create;
> MyVar.MyField:=ReadValue(FromGodKnowsWhere);
> MyList.Add(MyVar);
> end

> ...and it worked out. All the values where correct.

> My question is: TList is just a list of pointers, right?

Right.

Quote
> It holds no
> data, just a list of pointers to the adress where each item is, right?

Right.

Quote
> So, each item in MyList is actually just a pointer to an instance of
> MyVar. But I've been using the same variable over and over. I know that
> by some magical arts each time I use the MyVar:=TMyClass.Create I

There is not much magic about it. The Create returns a pointer the new
instance of the object which is then stored in MyVar.

Quote
> actually create another instance of the class, that will be sent to
> another place in the heap. BUT, how can I clean all that garbage when I
> want to free the memory?

You have a list of all the pointers stored in MyList.
So before freeing MyList you iterate thorugh MyList and for each pointer you
do a free.
Something like this:
  for i := 0 to MyList.Count - 1 do
    TMyClass(MyList.Items[i]).Free;
  MyList.Free;

Quote
> And if, as I believe, I'm just lucky that this worked out, because this
> construction should crash the program at the first place, what would be
> the correct way to do this?

You have it pretty well worked out, you only needed to free your objects.

Other Threads