list_destroy only destroys half the list

View: New views
1 Messages — Rating Filter:   Alert me  

list_destroy only destroys half the list

by Jonathan Matthew-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I noticed the list_destroy function in libvisual 0.4.0 only destroys
half of the list elements.

In this loop:

                while ((elem = visual_list_next (list, &le)) != NULL) {
                        destroyer (elem);
                        visual_list_delete (list, &le);
                }

both visual_list_next and visual_list_delete move 'le' to the next list
element.  The attached patch makes valgrind much happier.

enjoy,
-jonathan

(not subscribed; please cc me on responses)

diff -ur libvisual-0.4.0/libvisual/lv_list.c libvisual-0.4.0-fix/libvisual/lv_list.c
--- libvisual-0.4.0/libvisual/lv_list.c 2006-01-22 23:23:37.000000000 +1000
+++ libvisual-0.4.0-fix/libvisual/lv_list.c 2006-09-26 22:00:38.000000000 +1000
@@ -67,19 +67,22 @@
  VisCollectionDestroyerFunc destroyer;
  VisList *list = VISUAL_LIST (collection);
  VisListEntry *le = NULL;
- void *elem;
 
  visual_log_return_val_if_fail (list != NULL, -VISUAL_ERROR_COLLECTION_NULL);
 
  destroyer = visual_collection_get_destroyer (collection);
 
- /* Walk through the given list, possibly calling the destroyer for it */
+ /* Walk through the given list, possibly calling the destroyer for it.
+ * visual_list_delete moves the list entry pointer to the next entry.
+ */
+ visual_list_next (list, &le);
  if (destroyer == NULL) {
- while ((elem = visual_list_next (list, &le)) != NULL)
+ while (le != NULL) {
  visual_list_delete (list, &le);
+ }
  } else {
- while ((elem = visual_list_next (list, &le)) != NULL) {
- destroyer (elem);
+ while (le != NULL) {
+ destroyer (le->data);
  visual_list_delete (list, &le);
  }
  }

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Libvisual-devel mailing list
Libvisual-devel@...
https://lists.sourceforge.net/lists/listinfo/libvisual-devel
LightInTheBox - Buy quality products at wholesale price