Recent

Author Topic: Listbox.clear  (Read 6283 times)

clemmi

  • Jr. Member
  • **
  • Posts: 54
Listbox.clear
« on: September 02, 2014, 12:45:53 am »
After performing several operations in a ListBox using mainly Form1.ListBox1.items.add(...);
and then using Form1.ListsBox1.clear;
it always clears the display, but

sometimes when I run a procedure that adds other items to the now blank ListBox some of the previous items still show there in addition to the new ones.
How can I 'completely' clear the ListBox?

I read that some 'objects' may still remain after using ListBox.clear but I have no idea how to go about removing those lingering 'objects'.
Please clarify what is going on.
Thanks!



User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Listbox.clear
« Reply #1 on: September 02, 2014, 01:23:41 am »
That should be impossible. Internally same thing happens as when you do stringList.Clear; All the strings get completely destroyed from memory. You might be having some drawing issue, buffering or something else. In that case you need to tell your OS and Lazarus version.

Can you make a simple application where this bug happens, and then upload it as attachment?

You can also debug it by printing out the ListBox1.items.Text after every operation.
« Last Edit: September 02, 2014, 01:26:21 am by User137 »

nikolas

  • Newbie
  • Posts: 5
  • searching my best versión
Re: Listbox.clear
« Reply #2 on: September 02, 2014, 02:05:53 am »
try

listbox1.Items.Clear;   ;)

clemmi

  • Jr. Member
  • **
  • Posts: 54
Re: Listbox.clear
« Reply #3 on: September 02, 2014, 02:39:12 am »
ListBox1.items.clear did not solved the problem.

ListBox1.items.text did not showed anything after ListBox1.clear but on entering a new set of 3 lines all of the previous cleared lines also showed.

I can try to make a simple example but the actual whole program has several units and it would be difficult to post.

I'm using Windows 7, 64 bit but running a 32 bit Lazarus 1.0.14, Date 2013-11-16 - FPC version 2.6.2 - SVN version 43446 - i386-win32-win32/win64 (from the Lazarus About screen).

I use ListBox.clear in many places and never had this problem. I'll find the reference that mention 'objects' not cleared by Listbox.clear and post to see if it helps someone see what 's happening.
-------------------------------
Found it. The reference is from:

http://forum.codecall.net/topic/73705-the-basic-working-with-tlistbox/

"Note that deleting an item will not automatically free/destroy associated object. So if you have object associated with the deleted item, you need to employ another method to make sure the object will not becoming memory leak."

Actually, they are refereing to the use of ListBox.Items.Delete, not ListBox.clear. Don't know what 'another method to employ'.
--------------------------------
« Last Edit: September 02, 2014, 02:48:33 am by clemmi »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Listbox.clear
« Reply #4 on: September 02, 2014, 04:32:45 am »
"Note that deleting an item will not automatically free/destroy associated object. So if you have object associated with the deleted item, you need to employ another method to make sure the object will not becoming memory leak."

Actually, they are refereing to the use of ListBox.Items.Delete, not ListBox.clear. Don't know what 'another method to employ'.
--------------------------------
It has nothing to do with your problem. You can associate an object with an item. Like when you use:
Code: [Select]
  i := ListBox.Items.AddObject('Some Text', SomeObject);

and later you can get the object associated with an item:
Code: [Select]
  AssociatedObject := ListBox1.Items.Objects[i];

When you delete an item these objects need to be destroyed somehow:
Code: [Select]
  ListBox1.Items.Objects[i].Free;
  ListBox1.Items.Delete(i);

Basile B.

  • Guest
Re: Listbox.clear
« Reply #5 on: September 02, 2014, 04:52:10 am »
After performing several operations in a ListBox using mainly Form1.ListBox1.items.add(...);
and then using Form1.ListsBox1.clear;
it always clears the display, but

sometimes when I run a procedure that adds other items to the now blank ListBox some of the previous items still show there in addition to the new ones.
How can I 'completely' clear the ListBox?

I read that some 'objects' may still remain after using ListBox.clear but I have no idea how to go about removing those lingering 'objects'.
Please clarify what is going on.
Thanks!

If you have assigned something to the item, this is not impossible. The problem then would be that you haven't freed the objects or the pointers assigned to an item.
There is a special procedure for this. You can assign the event 'Ondeletion', then you 'll get a pointer to your item.data and you'll be able to free it, according to its type.

clemmi

  • Jr. Member
  • **
  • Posts: 54
Re: Listbox.clear
« Reply #6 on: September 02, 2014, 07:17:14 am »
Problem solved. My bad. But, I learned a few things in the process from the posts. Thanks you for all the responses.

 

TinyPortal © 2005-2018