diff --git a/lib/vector/Vlib/geos.c b/lib/vector/Vlib/geos.c
index 719e0fdd270..2c68a0a6fba 100644
--- a/lib/vector/Vlib/geos.c
+++ b/lib/vector/Vlib/geos.c
@@ -290,7 +290,7 @@ GEOSCoordSequence *V1_read_line_geos(struct Map_info *Map, long offset,
     long size;
     double *x, *y, *z;
 
-    GEOSCoordSequence *pseq;
+    GEOSCoordSequence *pseq = NULL;
 
     G_debug(3, "V1_read_line_geos(): offset = %ld", offset);
 
@@ -353,8 +353,6 @@ GEOSCoordSequence *V1_read_line_geos(struct Map_info *Map, long offset,
     G_debug(3, "    n_points = %d dim = %d", n_points,
             (Map->head.with_z) ? 3 : 2);
 
-    pseq = GEOSCoordSeq_create(n_points, (Map->head.with_z) ? 3 : 2);
-
     x = (double *)G_malloc(n_points * sizeof(double));
     y = (double *)G_malloc(n_points * sizeof(double));
     if (Map->head.with_z)
@@ -362,17 +360,22 @@ GEOSCoordSequence *V1_read_line_geos(struct Map_info *Map, long offset,
     else
         z = NULL;
 
-    if (0 >= dig__fread_port_D(x, n_points, &(Map->dig_fp)))
-        return NULL; /* end of file */
+    if (0 >= dig__fread_port_D(x, n_points, &(Map->dig_fp))) {
+        goto free_return; /* end of file */
+    }
 
-    if (0 >= dig__fread_port_D(y, n_points, &(Map->dig_fp)))
-        return NULL; /* end of file */
+    if (0 >= dig__fread_port_D(y, n_points, &(Map->dig_fp))) {
+        goto free_return; /* end of file */
+    }
 
     if (Map->head.with_z) {
-        if (0 >= dig__fread_port_D(z, n_points, &(Map->dig_fp)))
-            return NULL; /* end of file */
+        if (0 >= dig__fread_port_D(z, n_points, &(Map->dig_fp))) {
+            goto free_return; /* end of file */
+        }
     }
 
+    pseq = GEOSCoordSeq_create(n_points, (Map->head.with_z) ? 3 : 2);
+
     for (i = 0; i < n_points; i++) {
         GEOSCoordSeq_setX(pseq, i, x[i]);
         GEOSCoordSeq_setY(pseq, i, y[i]);
@@ -382,6 +385,7 @@ GEOSCoordSequence *V1_read_line_geos(struct Map_info *Map, long offset,
 
     G_debug(3, "    off = %ld", (long)dig_ftell(&(Map->dig_fp)));
 
+free_return:
     G_free((void *)x);
     G_free((void *)y);
     if (z)