Re: Copy TStringList to TListBox



Arjan 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:

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
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";

.



Relevant Pages

  • Re: A taxonomy of types
    ... however, elsewhere in my project (off in the dynamic typesystem, ...), I ... (since I am using NULL-terminated strings), and so I have used U+10FFFF ... remember, C also has things like arrays, funtion pointers, nestable ... int RIL_TypeSmallIntP; ...
    (comp.lang.misc)
  • Re: new IL: C (sort of...).
    ... only for "recent" Pascals, ... far pointers weren't really limited, ... in my compiler, I made wchar_t a builtin type (in most cases, aliased to ... I could very well include builtin "managed strings" in the new IL. ...
    (comp.lang.misc)
  • Re: HeapFree() Failing to deallocate string
    ... I've been able to recreate and isolate the problem with HeapFree(), ... in this simplified example is just a pointer to 16 bytes to store pointers ... the szCaption strings need to be copied to the pointed to ... strings for which storage needs to be allocated, ...
    (microsoft.public.windowsce.embedded)
  • Re: Increasing efficiency in C
    ... >> The representation of a string in C is the sequence of characters, ... strings, they are passed the addresses of strings. ... supports pointers the way it does. ... Competent programmers make mistakes, too. ...
    (comp.lang.c)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Would changing 'point to a' to 'point into a' twenty element array be ... > arrays of pointers is to store character strings of diverse ... comparison between what was really happening (arrays of pointers to ... pointer to a string(this probably would confuse beginners)" and ...
    (comp.lang.c)