--- dia-0.97/app/load_save.c	2009-04-17 09:25:23.000000000 +0100
+++ dia-0.97-2pass/app/load_save.c	2009-12-03 09:44:52.835041207 +0000
@@ -237,11 +237,10 @@
 }
 
 static void
-read_connections(GList *objects, xmlNodePtr layer_node,
-		 GHashTable *objects_hash)
+read_connections(GList *list, xmlNodePtr layer_node,
+                 GHashTable *objects_hash)
 {
   ObjectNode obj_node;
-  GList *list;
   xmlNodePtr connections;
   xmlNodePtr connection;
   char *handlestr;
@@ -250,7 +249,6 @@
   int handle, conn;
   DiaObject *to;
   
-  list = objects;
   obj_node = layer_node->xmlChildrenNode;
   while ((list != NULL) && (obj_node != NULL)) {
     DiaObject *obj = (DiaObject *) list->data;
@@ -271,6 +269,7 @@
       while ((connections!=NULL) &&
 	     (xmlStrcmp(connections->name, (const xmlChar *)"connections")!=0))
 	connections = connections->next;
+      
       if (connections != NULL) {
 	connection = connections->xmlChildrenNode;
 	while (connection != NULL) {
@@ -410,6 +409,7 @@
   Diagram *diagram = DIA_IS_DIAGRAM (data) ? DIA_DIAGRAM (data) : NULL;
   Layer *active_layer = NULL;
   GHashTable* unknown_objects_hash = g_hash_table_new(g_str_hash, g_str_equal);
+  int i;
 
 
   if (g_file_test (filename, G_FILE_TEST_IS_DIR)) {
@@ -613,7 +613,8 @@
     find_node_named (doc->xmlRootNode->xmlChildrenNode, "layer");
 
   objects_hash = g_hash_table_new(g_str_hash, g_str_equal);
-
+  
+  /* 1st pass: read in all objects for each layer */
   while (layer_node != NULL) {
     gchar *name;
     char *visible;
@@ -630,6 +631,9 @@
     if (!name) break; /* name is mandatory */
 
     layer = new_layer(g_strdup(name), data);
+    /* set for connection processing below */
+    layer->node = layer_node;
+    
     if (name) xmlFree(name);
 
     layer->visible = FALSE;
@@ -639,11 +643,10 @@
         layer->visible = TRUE;
       xmlFree(visible);
     }
-    /* Read in all objects: */
     
+    /* Read in all objects: */
     list = read_objects(layer_node, objects_hash, filename, NULL, unknown_objects_hash);
     layer_add_objects (layer, list);
-    read_connections( list, layer_node, objects_hash);
 
     data_add_layer(data, layer);
 
@@ -656,6 +659,18 @@
 
     layer_node = layer_node->next;
   }
+  
+  /* 2nd pass - connections */
+  for (i = 0; i < data->layers->len; i++) {
+    layer = (Layer *) g_ptr_array_index(data->layers, i);
+    
+    read_connections (layer->objects, layer->node, objects_hash);
+    
+    /* invalidate the layer XML node pointer, so we can safely free the doc
+     * below 
+     */
+    layer->node = NULL;
+  }
 
   if (!active_layer)
     data->active_layer = (Layer *) g_ptr_array_index(data->layers, 0);
--- dia-0.97/lib/diagramdata.h	2009-04-17 09:25:23.000000000 +0100
+++ dia-0.97-2pass/lib/diagramdata.h	2009-12-03 09:39:14.047790361 +0000
@@ -109,7 +109,10 @@
 			     sorted by decreasing z-valued,
 			     objects can ONLY be connected to objects
 			     in the same layer! */
-
+  void *node;		/* Convenience pointer to corresponding XML Node, if
+                         * there is an XML document associated. May only
+                         * be valid during load/save
+                         */
   gboolean visible;
   gboolean connectable;   /*!< Whether the layer can currently be connected to.
 			     The selected layer is by default connectable */
