[patch] tsearch.3: dispose properly of allocated resources in example code

From: André Goddard Rosa <hidden>
Date: 2008-09-21 06:10:43

As there are many users who base their own code on these examples, we should avoid leaking memory when using functions of tsearch() family.
In the provided example, tsearch() leaks memory whenever it tries to insert a number which was already inserted before.

Please CC me, as I´m not subscribed.

Applies to man-pages-3.09.
--- man3/tsearch.3	2008-09-10 10:16:13.000000000 -0300
+++ man3/tsearch.3.goddard	2008-09-21 02:42:55.000000000 -0300
@@ -246,8 +246,11 @@
         val = tsearch((void *) ptr, &root, compare);
         if (val == NULL)
             exit(EXIT_FAILURE);
+        else if (!((*(int **) val) == ptr))
+            free(ptr);
     }
     twalk(root, action);
+    tdestroy(root, free);
     exit(EXIT_SUCCESS);
 }
 .fi
  

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help