leakfix
[oweals/gnunet.git] / src / util / container_heap.c
index 1e7077c8003ea19d2a7fe134707971ba7ac2462d..b5ccd795051c3f8bb80fa06f414eb149ca7200d0 100644 (file)
@@ -331,6 +331,8 @@ GNUNET_CONTAINER_heap_remove_node (struct GNUNET_CONTAINER_Heap *root,
 
   if (del_node == NULL)
     return NULL;
+  else if (del_node == root->root)
+    return GNUNET_CONTAINER_heap_remove_root (root);
 
   ret = del_node->element;
   last = getPos (root, root->size);
@@ -410,6 +412,15 @@ GNUNET_CONTAINER_heap_remove_root (struct GNUNET_CONTAINER_Heap *root)
   ret = root_node->element;
   last = getPos (root, root->size);
 
+  if ((root_node == last) && (root->size == 1)) 
+    {
+      /* We are removing the last node in the heap! */
+      root->root = NULL;
+      root->traversal_pos = NULL;
+      root->size = 0;
+      return ret;
+    }
+
   if (last->parent->left_child == last)
     last->parent->left_child = NULL;
   else if (last->parent->right_child == last)