[GRASS-SVN] r35667 - in grass/trunk: general/g.proj include lib/proj

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jan 29 10:29:28 EST 2009


Author: pkelly
Date: 2009-01-29 10:29:28 -0500 (Thu, 29 Jan 2009)
New Revision: 35667

Modified:
   grass/trunk/general/g.proj/Makefile
   grass/trunk/general/g.proj/datumtrans.c
   grass/trunk/general/g.proj/output.c
   grass/trunk/include/gprojects.h
   grass/trunk/lib/proj/convert.c
   grass/trunk/lib/proj/datum.c
   grass/trunk/lib/proj/ellipse.c
   grass/trunk/lib/proj/get_proj.c
Log:
Use pj_dalloc() to free string allocated by pj_get_def() [fixes bug 468].
Fix some memory leaks in proj-related stuff.


Modified: grass/trunk/general/g.proj/Makefile
===================================================================
--- grass/trunk/general/g.proj/Makefile	2009-01-28 12:17:43 UTC (rev 35666)
+++ grass/trunk/general/g.proj/Makefile	2009-01-29 15:29:28 UTC (rev 35667)
@@ -3,7 +3,7 @@
 PGM = g.proj
 
 EXTRA_INC = $(PROJINC)
-LIBES     = $(GPROJLIB) $(GISLIB)
+LIBES     = $(PROJLIB) $(GPROJLIB) $(GISLIB)
 DEPENDENCIES= $(GPROJDEP) $(GISDEP)
 
 include $(MODULE_TOPDIR)/include/Make/Module.make

Modified: grass/trunk/general/g.proj/datumtrans.c
===================================================================
--- grass/trunk/general/g.proj/datumtrans.c	2009-01-28 12:17:43 UTC (rev 35666)
+++ grass/trunk/general/g.proj/datumtrans.c	2009-01-29 15:29:28 UTC (rev 35667)
@@ -42,15 +42,16 @@
 {
     char *params, *datum = NULL;
     int paramsets, status;
-    struct gpj_datum dstruct;
 
     if (cellhd.proj == PROJECTION_XY)
 	return 0;
 
     status = GPJ__get_datum_params(projinfo, &datum, &params);
+    G_free(params);
     if (datum) {
 	/* A datum name is specified; need to determine if
 	 * there are parameters to choose from for this datum */
+	struct gpj_datum dstruct;
 
 	if (GPJ_get_datum_by_name(datum, &dstruct) > 0) {
 	    char *defparams;
@@ -58,6 +59,8 @@
 	    paramsets =
 		GPJ_get_default_datum_params_by_name(dstruct.name,
 						     &defparams);
+	    G_free(defparams);
+	    GPJ_free_datum(&dstruct);
 
 	    if (status == 1 && paramsets > 1)
 		/* Parameters are missing and there is a choice to be made */
@@ -94,21 +97,23 @@
 	    if (list != NULL) {
 		if (datumtrans == -1) {
 		    do {
+			struct gpj_datum_transform_list *old = list;
 			fprintf(stdout, "---\n%d\nUsed in %s\n%s\n%s\n",
 				list->count, list->where_used,
 				list->params, list->comment);
 			list = list->next;
+		        GPJ_free_datum_transform(old);
 		    } while (list != NULL);
 
 		    exit(EXIT_SUCCESS);
 		}
 		else {
 		    do {
-			if (list->count == datumtrans) {
+			struct gpj_datum_transform_list *old = list;
+			if (list->count == datumtrans)
 			    chosenparams = G_store(list->params);
-			    break;
-			}
 			list = list->next;
+		        GPJ_free_datum_transform(old);
 		    } while (list != NULL);
 		}
 	    }
@@ -146,6 +151,7 @@
 	projinfo = temp_projinfo;
 
     }
+    G_free(datum);
 
     return force;
 }

Modified: grass/trunk/general/g.proj/output.c
===================================================================
--- grass/trunk/general/g.proj/output.c	2009-01-28 12:17:43 UTC (rev 35666)
+++ grass/trunk/general/g.proj/output.c	2009-01-29 15:29:28 UTC (rev 35667)
@@ -17,6 +17,9 @@
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
+
+#include <proj_api.h>
+
 #include <grass/gis.h>
 #include <grass/gprojects.h>
 #include <grass/glocale.h>
@@ -102,6 +105,7 @@
 
     pj_get_kv(&pjinfo, projinfo, projunits);
     proj4 = pj_get_def(pjinfo.pj, 0);
+    pj_free(pjinfo.pj);
 
     /* GRASS-style PROJ.4 strings don't include a unit factor as this is
      * handled separately in GRASS - must include it here though */
@@ -109,7 +113,8 @@
     if (unfact != NULL && (strcmp(pjinfo.proj, "ll") != 0))
 	G_asprintf(&proj4mod, "%s +to_meter=%s", proj4, unfact);
     else
-	proj4mod = proj4;
+	proj4mod = G_store(proj4);
+    pj_dalloc(proj4);
 
     for (i = proj4mod; *i; i++) {
 	/* Don't print the first space */
@@ -122,6 +127,7 @@
 	    fputc(*i, stdout);
     }
     fputc('\n', stdout);
+    G_free(proj4mod);
 
     return;
 }

Modified: grass/trunk/include/gprojects.h
===================================================================
--- grass/trunk/include/gprojects.h	2009-01-28 12:17:43 UTC (rev 35666)
+++ grass/trunk/include/gprojects.h	2009-01-29 15:29:28 UTC (rev 35667)
@@ -95,8 +95,8 @@
 int GPJ_get_datum_params(char **, char **);
 int GPJ__get_datum_params(struct Key_Value *, char **, char **);
 void GPJ_free_datum(struct gpj_datum *);
-struct gpj_datum_transform_list *GPJ_get_datum_transform_by_name(const char
-								 *inputname);
+struct gpj_datum_transform_list *GPJ_get_datum_transform_by_name(const char *);
+void GPJ_free_datum_transform(struct gpj_datum_transform_list *);
 
 /* ellipse.c */
 int GPJ_get_ellipsoid_by_name(const char *, struct gpj_ellps *);

Modified: grass/trunk/lib/proj/convert.c
===================================================================
--- grass/trunk/lib/proj/convert.c	2009-01-28 12:17:43 UTC (rev 35666)
+++ grass/trunk/lib/proj/convert.c	2009-01-29 15:29:28 UTC (rev 35667)
@@ -93,9 +93,9 @@
     struct gpj_datum dstruct;
     struct gpj_ellps estruct;
     size_t len;
-    const char *ellps, *unit, *unfact;
-    char *ellpslong, *datum, *params, *towgs84, *datumlongname,
-	*start, *end, *buff;
+    const char *ellpskv, *unit, *unfact;
+    char *ellps, *ellpslong, *datum, *params, *towgs84, *datumlongname,
+	*start, *end;
     const char *sysname, *osrunit, *osrunfact;
     double a, es, rf;
     int haveparams = 0;
@@ -114,19 +114,22 @@
 	G_warning(_("Unable get PROJ.4-style parameter string"));
 	return NULL;
     }
+    pj_free(pjinfo.pj);
 
     unit = G_find_key_value("unit", proj_units);
     unfact = G_find_key_value("meters", proj_units);
     if (unfact != NULL && (strcmp(pjinfo.proj, "ll") != 0))
 	G_asprintf(&proj4mod, "%s +to_meter=%s", proj4, unfact);
     else
-	proj4mod = proj4;
+	proj4mod = G_store(proj4);
+    pj_dalloc(proj4);
 
     if ((errcode = OSRImportFromProj4(hSRS, proj4mod)) != OGRERR_NONE) {
 	G_warning(_("OGR can't parse PROJ.4-style parameter string: "
 		    "%s (OGR Error code was %d)"), proj4mod, errcode);
 	return NULL;
     }
+    G_free(proj4mod);
 
     if ((errcode = OSRExportToWkt(hSRS, &wkt)) != OGRERR_NONE) {
 	G_warning(_("OGR can't get WKT-style parameter string "
@@ -134,14 +137,19 @@
 	return NULL;
     }
 
-    ellps = G_find_key_value("ellps", proj_info);
+    ellpskv = G_find_key_value("ellps", proj_info);
     GPJ__get_ellipsoid_params(proj_info, &a, &es, &rf);
     haveparams = GPJ__get_datum_params(proj_info, &datum, &params);
 
+    if(ellpskv != NULL)
+	ellps = G_store(ellpskv);
+    else
+	ellps = NULL;
+
     if ((datum == NULL) || (GPJ_get_datum_by_name(datum, &dstruct) < 0)) {
-	datumlongname = "unknown";
+	datumlongname = G_store("unknown");
 	if (ellps == NULL)
-	    ellps = "unnamed";
+	    ellps = G_store("unnamed");
     }
     else {
 	datumlongname = G_store(dstruct.longname);
@@ -149,6 +157,7 @@
 	    ellps = G_store(dstruct.ellps);
 	GPJ_free_datum(&dstruct);
     }
+    G_free(datum);
     if (GPJ_get_ellipsoid_by_name(ellps, &estruct) > 0) {
 	ellpslong = G_store(estruct.longname);
 	DatumNameMassage(&ellpslong);
@@ -171,16 +180,17 @@
 	if (G_strcasecmp(paramkey, "towgs84") == 0)
 	    G_asprintf(&towgs84, ",TOWGS84[%s]", paramvalue);
 	else
-	    towgs84 = "";
+	    towgs84 = G_store("");
+	G_free(params);
     }
     else
-	towgs84 = "";
+	towgs84 = G_store("");
 
     sysname = OSRGetAttrValue(hSRS, "PROJCS", 0);
     if (sysname == NULL) {
 	/* Not a projected co-ordinate system */
-	start = "";
-	end = "";
+	start = G_store("");
+	end = G_store("");
     }
     else {
 	if ((strcmp(sysname, "unnamed") == 0) &&
@@ -194,8 +204,9 @@
 	osrunfact = OSRGetAttrValue(hSRS, "UNIT", 1);
 
 	if ((unfact == NULL) || (G_strcasecmp(osrunit, "unknown") != 0))
-	    end = "";
+	    end = G_store("");
 	else {
+	    char *buff;
 	    double unfactf = atof(unfact);
 
 	    G_asprintf(&buff, ",UNIT[\"%s\",", osrunit);
@@ -203,6 +214,7 @@
 	    startmod = strstr(lastpart, buff);
 	    len = strlen(lastpart) - strlen(startmod);
 	    lastpart[len] = '\0';
+	    G_free(buff);
 
 	    if (unit == NULL)
 		unit = "unknown";
@@ -210,27 +222,21 @@
 	}
 
     }
-
+    OSRDestroySpatialReference(hSRS);
     G_asprintf(&modwkt,
 	       "%sGEOGCS[\"%s\",DATUM[\"%s\",SPHEROID[\"%s\",%.16g,%.16g]%s],%s%s",
 	       start, ellps, datumlongname, ellpslong, a, rf, towgs84,
 	       lastpart, end);
-
     hSRS2 = OSRNewSpatialReference(modwkt);
+    G_free(modwkt);
 
-    OSRDestroySpatialReference(hSRS);
-    G_free(modwkt);
     CPLFree(wkt);
-    if (proj4 != proj4mod)
-	G_free(proj4);
-    G_free(proj4mod);
-    G_free(datum);
-    G_free(params);
+    G_free(start);
+    G_free(ellps);
     G_free(datumlongname);
-    pj_free(pjinfo.pj);
     G_free(ellpslong);
-    /* Other string pointers may or may not need to be freed depending
-     * on sequence of execution so just leave them. */
+    G_free(towgs84);
+    G_free(end);
 
     return hSRS2;
 }
@@ -451,13 +457,11 @@
 
 			if (list != NULL) {
 			    do {
-				if (list->count == datumtrans) {
+				if (list->count == datumtrans)
 				    chosenparams = G_store(list->params);
-				    break;
-				}
 				old = list;
 				list = list->next;
-				G_free(old);
+				GPJ_free_datum_transform(old);
 			    } while (list != NULL);
 			}
 		    }
@@ -476,6 +480,7 @@
 		}
 
 	    }
+	    G_free(pszDatumName);
 	}
     }
 

Modified: grass/trunk/lib/proj/datum.c
===================================================================
--- grass/trunk/lib/proj/datum.c	2009-01-28 12:17:43 UTC (rev 35666)
+++ grass/trunk/lib/proj/datum.c	2009-01-29 15:29:28 UTC (rev 35667)
@@ -85,7 +85,7 @@
 int GPJ_get_default_datum_params_by_name(const char *name, char **params)
 {
     struct gpj_datum_transform_list *list, *old;
-    int count = 1;
+    int count = 0;
 
     list = GPJ_get_datum_transform_by_name(name);
 
@@ -98,14 +98,13 @@
      * (will normally be a 3-parameter transformation)        */
     *params = G_store(list->params);
 
-    while (list->next != NULL) {
+    while (list != NULL) {
 	count++;
 	old = list;
 	list = list->next;
-	G_free(old);
+	GPJ_free_datum_transform(old);
     }
 
-    G_free(list);
     return count;
 
 }
@@ -311,6 +310,21 @@
 }
 
 /**
+ * \brief Free the memory used by a gpj_datum_transform_list struct
+ *
+ * \param item gpj_datum_transform_list struct to be freed
+ **/
+
+void GPJ_free_datum_transform(struct gpj_datum_transform_list *item)
+{
+    G_free(item->params);
+    G_free(item->where_used);
+    G_free(item->comment);
+    G_free(item);
+    return;
+}
+
+/**
  * \brief Read the current GRASS datum.table from disk and store in
  *        memory
  * 

Modified: grass/trunk/lib/proj/ellipse.c
===================================================================
--- grass/trunk/lib/proj/ellipse.c	2009-01-28 12:17:43 UTC (rev 35666)
+++ grass/trunk/lib/proj/ellipse.c	2009-01-29 15:29:28 UTC (rev 35667)
@@ -55,7 +55,7 @@
     struct gpj_ellps estruct;
     struct gpj_datum dstruct;
     const char *str, *str3;
-    char *str1;
+    char *str1, *ellps;
 
     str = G_find_key_value("datum", proj_keys);
 
@@ -63,25 +63,25 @@
 	/* If 'datum' key is present, look up correct ellipsoid
 	 * from datum.table */
 
-	str = G_store(dstruct.ellps);
+	ellps = G_store(dstruct.ellps);
 	GPJ_free_datum(&dstruct);
 
     }
     else
 	/* else use ellipsoid defined in PROJ_INFO */
-	str = G_find_key_value("ellps", proj_keys);
+	ellps = G_store(G_find_key_value("ellps", proj_keys));
 
-    if (str != NULL) {
-	if (GPJ_get_ellipsoid_by_name(str, &estruct) < 0) {
-	    G_fatal_error(_("Invalid ellipsoid <%s> in file"), str);
-	}
-	else {
-	    *a = estruct.a;
-	    *e2 = estruct.es;
-	    *rf = estruct.rf;
-	    GPJ_free_ellps(&estruct);
-	    return 1;
-	}
+    if (ellps != NULL) {
+	if (GPJ_get_ellipsoid_by_name(ellps, &estruct) < 0)
+	    G_fatal_error(_("Invalid ellipsoid <%s> in file"), ellps);
+
+	*a = estruct.a;
+	*e2 = estruct.es;
+	*rf = estruct.rf;
+	GPJ_free_ellps(&estruct);
+	G_free(ellps);
+
+	return 1;
     }
     else {
 	str3 = G_find_key_value("a", proj_keys);

Modified: grass/trunk/lib/proj/get_proj.c
===================================================================
--- grass/trunk/lib/proj/get_proj.c	2009-01-28 12:17:43 UTC (rev 35666)
+++ grass/trunk/lib/proj/get_proj.c	2009-01-29 15:29:28 UTC (rev 35667)
@@ -213,12 +213,12 @@
 	    alloc_options(buffa);
 	    returnval = 3;
 	}
-	G_free(datum);
 	/* else there'll be no datum transformation taking place here... */
     }
     else {
 	returnval = 4;
     }
+    G_free(datum);
 
     /* Set finder function for locating datum conversion tables PK */
     pj_set_finder(FINDERFUNC);



More information about the grass-commit mailing list