diff --git a/src/protobuf.c b/src/protobuf.c
index 1e66cc3463b7d859103185d27d64d8272da02484..21959eceb10256bebf3e360f687afd7ac575d683 100644
--- a/src/protobuf.c
+++ b/src/protobuf.c
@@ -1338,13 +1338,13 @@ parse_pbf_nodes (readosm_string_table * strings,
 	  int max_nodes;
 	  int base = 0;
 	  nd_count = packed_ids.count;
+          nodes = malloc (sizeof (readosm_internal_node) * MAX_NODES);
 	  while (base < nd_count)
 	    {
 		/* processing about 1024 nodes at each time */
 		max_nodes = MAX_NODES;
 		if ((nd_count - base) < MAX_NODES)
 		    max_nodes = nd_count - base;
-		nodes = malloc (sizeof (readosm_internal_node) * max_nodes);
 		for (i = 0; i < max_nodes; i++)
 		  {
 		      /* initializing an array of empty internal Nodes */
@@ -1461,9 +1461,9 @@ parse_pbf_nodes (readosm_string_table * strings,
 			    nd = nodes + i;
 			    destroy_internal_node (nd);
 			}
-		      free (nodes);
 		  }
 	    }
+          free(nodes);
       }
 
 /* memory cleanup */