瀏覽代碼

Fixed potential memory leak in integer list init, added new and free ilist functions.

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@57595 15284696-431f-4ddb-bdfa-cd5b030d7da7
Soeren Gebbert 11 年之前
父節點
當前提交
5120adf63f
共有 2 個文件被更改,包括 35 次插入1 次删除
  1. 2 0
      include/defs/gis.h
  2. 33 1
      lib/gis/ilist.c

+ 2 - 0
include/defs/gis.h

@@ -321,6 +321,8 @@ const char *G__home(void);
 
 /* ilist.c */
 void G_init_ilist(struct ilist *);
+void G_free_ilist(struct ilist *);
+struct ilist * G_new_ilist();
 void G_ilist_add(struct ilist *, int);
 
 /* intersect.c */

+ 33 - 1
lib/gis/ilist.c

@@ -18,14 +18,46 @@
 #include <stdlib.h>
 #include <grass/gis.h>
 
+/**
+ * \brief Free allocated memory of an integer list
+ *
+ * \param list The pointer to an integer list
+ *
+ * */
+void G_free_ilist(struct ilist *list)
+{
+    if(list->value)
+        G_free(list->value);
+    G_free(list);
+}
+
+/**
+ * \brief Return a new integer list.
+ * 
+ * G_fatal_error() will be invoked by the
+ * allocation function.
+ *
+ * \return list The pointer to a new allocated integer list
+ *
+ * */
+struct ilist * G_new_ilist()
+{
+    struct ilist *l = G_malloc(sizeof(struct ilist));
+    l->value = NULL;
+    G_init_ilist(l);
+    return l;
+}
+
 /** 
- * Init an integer list  
+ * \brief Init an integer list and free allocated memory
  *
  * \param list The pointer to an integer list
  *
  * */
 void G_init_ilist(struct ilist *list)
 {
+    if(list->value)
+        G_free(list->value);
     list->value = NULL;
     list->n_values = 0;
     list->alloc_values = 0;