Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copy TStringList to TListBox

2,071 views
Skip to first unread message

Arjan

unread,
Oct 2, 2008, 6:39:54 PM10/2/08
to
Hello all,

I have a TStringList containing some strings, with associated objects.
I wish to copy this list to a TListBox, so that the Listbox contains
a separate copy of the Stringlist items (including the objects).
I use Assign, but it seems that this only assigns pointers to the original
objects in the Stringlist instead of copying them to new objects:

ListBox1.Clear;
ListBox1.Items.Assign(StringList1);
StringList1.Clear; //this changes >=1 objects in ListBox1!

(The objects contain a record with string fields, I can watch them in the
debugger. One of the strings of at least one of the objects changes.)
Am I missing something basic?

Regards,
Arjan

Jamie

unread,
Oct 2, 2008, 8:37:48 PM10/2/08
to
Arjan wrote:

Ok, I did some looking here.
the Objects List simply add's the pointer list to reference the
objects. its the same pointers of course, this is why you are
seeing this.
The object array can only hold pointers or integer's etc, so when you
did that, it simply copied the pointers which is what it does..

This could lead to some issues if you free those pointers some
where..
You must remember that freeing the TstringList or what ever, does
not clean up the pointers in the object list. Those pointers will
stay alive.!
This is do to the fact that you can use that array to store other
items, not just pointers to objects.

I would say that maybe you should keep a TObjectList lying around
to maintain the original object list. This way, freeing the TObjectList
will also attempt to free the objects in the list.

You can associate them with any object list in the Tstrings classes.
etc..
Now, it's possible the Object array as changed from a Tlist to a
TobjectList. If this is the case, this could cause some issues if its
allowed to release the objects.. I don't know about this because I
haven't explored the source code of newer versions of delphi in some
time now. I've been stuck with D7 and waiting for them to produce a
new version that looks ready for mission control apps!

http://webpages.charter.net/jamie_5"

Hans-Peter Diettrich

unread,
Oct 2, 2008, 8:17:48 PM10/2/08
to
Arjan schrieb:

> I have a TStringList containing some strings, with associated objects.
> I wish to copy this list to a TListBox, so that the Listbox contains
> a separate copy of the Stringlist items (including the objects).
> I use Assign, but it seems that this only assigns pointers to the original
> objects in the Stringlist instead of copying them to new objects:

Objects[] contains references (pointers) to instances. Only the
references are copied. If you want to clone objects, you'll have do do
that in explicit code.

DoDi

alang...@aol.com

unread,
Oct 3, 2008, 6:19:13 AM10/3/08
to
On 2 Oct, 23:39, Arjan <ar...@ajcbkx.demon.nl> wrote:
> Hello all,
>
> I have a TStringList containing some strings, with associated objects.
> I wish to copy this list to a TListBox, so that the Listbox contains
> a separate copy of the Stringlist items (including the objects).
> I use Assign, but it seems that this only assigns pointers to the original
> objects in the Stringlist instead of copying them to new objects:
>

Not quite. Assign copies the values which are in the
TStringList.Objects array.

These are normally pointers to objects (ie a TObject refererence) but
can be any 4-byte value typecast to a TObject.

Hence you can free the TStringList object (itself not its Objects
array) you have Assign(ed) to the listbox, and the listbox still
maintains the original pointers (TObject references) which were in the
TStringList.Objects.

> StringList1.Clear; //this changes >=1 objects in ListBox1!

Not in my experience (D3)

... or have I misunderstood you.

Alan Lloyd

Arjan

unread,
Oct 3, 2008, 5:10:43 PM10/3/08
to
In message <78b1374c-a641-42f9...@k7g2000hsd.googlegroups.com>
"alang...@aol.com" <alang...@aol.com> wrote:

> On 2 Oct, 23:39, Arjan <ar...@ajcbkx.demon.nl> wrote:
> > Hello all,
> >
> > I have a TStringList containing some strings, with associated objects.
> > I wish to copy this list to a TListBox, so that the Listbox contains
> > a separate copy of the Stringlist items (including the objects).
> > I use Assign, but it seems that this only assigns pointers to the original
> > objects in the Stringlist instead of copying them to new objects:
>
>
> Not quite. Assign copies the values which are in the
> TStringList.Objects array.
>
> These are normally pointers to objects (ie a TObject refererence) but
> can be any 4-byte value typecast to a TObject.

[snip]



> > StringList1.Clear; //this changes >=1 objects in ListBox1!
>
> Not in my experience (D3)
>
> .. or have I misunderstood you.


No, my mistake.
I had made a TStringList descendant with a new Clear (overriding the
original) which Frees all the objects, then calls the original Clear. Sorry,
I was confused.

Thanks everyone.
Now I know that Assign only copies pointers, I have mended my code.
Everything works!

Regards,
Arjan

0 new messages