Index: pan/gui/pan-tree.h
===================================================================
--- pan/gui/pan-tree.h	(revision 138)
+++ pan/gui/pan-tree.h	(working copy)
@@ -385,7 +385,10 @@
         If this is null, we just call `delete row;' */
     RowDispose * row_dispose;
 
+    bool the_kids_are_alright (Row *);
+    bool row_is_in_tree (Row *);
 
+
   private: // gobject
     static void pan_tree_dispose (GObject *);
     static void pan_tree_finalize (GObject *);
Index: pan/gui/pan-tree.cc
===================================================================
--- pan/gui/pan-tree.cc	(revision 138)
+++ pan/gui/pan-tree.cc	(working copy)
@@ -147,7 +147,7 @@
 PanTreeStore :: model_get_n_columns (GtkTreeModel * model)
 {
   const PanTreeStore * store (PAN_TREE_STORE(model));
-  g_return_val_if_fail (store, 0);
+  g_assert (store);
   return store->n_columns;
 }
 
@@ -164,8 +164,8 @@
                                 GtkTreePath  * path)
 {
   PanTreeStore * store = PAN_TREE_STORE(model);
-  g_return_val_if_fail (store, false);
-  g_return_val_if_fail (path, false);
+  g_assert (store);
+  g_assert (path);
 
   // make sure it's not an empty path.
   const int depth (gtk_tree_path_get_depth (path));
@@ -191,7 +191,7 @@
                                 GtkTreeIter  * iter)
 {
   PanTreeStore * store (PAN_TREE_STORE(model));
-  g_return_val_if_fail (store, 0);
+  g_assert (store);
   return store->get_path (iter);
 }
 
@@ -216,9 +216,9 @@
                                  GtkTreeIter  * iter)
 {
   PanTreeStore * tree = PAN_TREE_STORE(model);
-  g_return_val_if_fail (tree, false);
-  g_return_val_if_fail (iter, false);
-  g_return_val_if_fail (iter->stamp == tree->stamp, false);
+  g_assert (tree);
+  g_assert (iter);
+  g_assert (iter->stamp == tree->stamp);
 
   Row * row (tree->get_row (iter));
   row = row->parent->nth_child (row->child_index + 1);
@@ -244,8 +244,8 @@
                                        GtkTreeIter  * iter)
 {
   PanTreeStore * tree = PAN_TREE_STORE(model);
-  g_return_val_if_fail (tree, 0);
-  g_return_val_if_fail (!iter || iter->stamp == tree->stamp, 0);
+  g_assert (tree);
+  g_assert (!iter || iter->stamp == tree->stamp);
 
   const Row * row (iter ? tree->get_row(iter) : tree->root);
   return row->n_children();
@@ -265,9 +265,9 @@
                                       gint           n)
 {
   PanTreeStore * tree = PAN_TREE_STORE(model);
-  g_return_val_if_fail (tree, false);
-  g_return_val_if_fail (iter, false);
-  g_return_val_if_fail (!parent || parent->stamp == tree->stamp, false);
+  g_assert (tree);
+  g_assert (iter);
+  g_assert (!parent || parent->stamp == tree->stamp);
 
   Row * row (parent ? tree->get_row(parent) : tree->root);
   row = row->nth_child (n);
@@ -295,7 +295,7 @@
 GtkTreePath*
 PanTreeStore :: get_path (const Row * row) const
 {
-  g_return_val_if_fail (row, false);
+  g_assert (row);
 
   std::vector<int> indices;
   while (row && row!=root) {
@@ -313,8 +313,8 @@
 GtkTreePath*
 PanTreeStore :: get_path (GtkTreeIter  * iter)
 {
-  g_return_val_if_fail (iter, false);
-  g_return_val_if_fail (iter->stamp == stamp, false);
+  g_assert (iter);
+  g_assert (iter->stamp == stamp);
 
   return get_path (get_row (iter));
 }
@@ -323,9 +323,9 @@
 PanTreeStore :: get_parent (GtkTreeIter * iter,
                             GtkTreeIter * child)
 {
-  g_return_val_if_fail (child, false);
-  g_return_val_if_fail (iter, false);
-  g_return_val_if_fail (child->stamp == stamp, false);
+  g_assert (child);
+  g_assert (iter);
+  g_assert (child->stamp == stamp);
 
   const Row * row (get_row (child));
   const bool has_public_parent (row->parent != root);
@@ -339,8 +339,8 @@
 bool
 PanTreeStore :: is_root (const GtkTreeIter* iter) const
 {
-  g_return_val_if_fail (iter, false);
-  g_return_val_if_fail (iter->stamp == stamp, false);
+  g_assert (iter);
+  g_assert (iter->stamp == stamp);
   return get_row(iter)->parent == root;
 }
 
@@ -378,7 +378,7 @@
                                              GtkSortType      * setme_order)
 {
   PanTreeStore * tree (PAN_TREE_STORE (sortable));
-  g_return_val_if_fail (tree, false);
+  g_assert (tree);
 
   if (setme_column_id)
      *setme_column_id = tree->sort_column_id;
@@ -393,7 +393,7 @@
 PanTreeStore :: sortable_has_sort_func (GtkTreeSortable *sortable, gint col)
 {
   PanTreeStore * store (PAN_TREE_STORE (sortable));
-  g_return_val_if_fail (store, false);
+  g_assert (store);
   column_sort_info_t::const_iterator it (store->sort_info->find (col));
   return (it!=store->sort_info->end()) && (it->second.sort_func!=0);
 }
@@ -527,7 +527,7 @@
                                              GtkSortType       order)
 {
   PanTreeStore * tree (PAN_TREE_STORE (sortable));
-  g_return_if_fail (tree);
+  g_assert (tree);
 
   // if no change, there's nothing to do...
   if (tree->sort_column_id==sort_column_id && tree->order==order)
@@ -535,8 +535,8 @@
 
   // sanity checks...
   if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID) {
-    g_return_if_fail (tree->sort_info->count(sort_column_id) != 0);
-    g_return_if_fail (tree->sort_info->find(sort_column_id)->second.sort_func != 0);
+    g_assert (tree->sort_info->count(sort_column_id) != 0);
+    g_assert (tree->sort_info->find(sort_column_id)->second.sort_func != 0);
   }
 
   const bool flip (sort_column_id == tree->sort_column_id);
@@ -561,7 +561,7 @@
                                         GtkDestroyNotify        destroy)
 {
   PanTreeStore * tree (PAN_TREE_STORE (sortable));
-  g_return_if_fail (tree);
+  g_assert (tree);
 
   (*tree->sort_info)[col].assign (func, data, destroy);
 
@@ -727,7 +727,7 @@
 PanTreeStore*
 PanTreeStore :: new_tree (int n_columns, ...)
 {
-  g_return_val_if_fail (n_columns>0, 0);
+  g_assert (n_columns>0);
   PanTreeStore* tree = (PanTreeStore*) g_object_new (PAN_TREE_STORE_TYPE, NULL);
 
   va_list args;
@@ -749,6 +749,7 @@
 void
 PanTreeStore :: row_changed (Row * row)
 {
+  g_assert (row_is_in_tree (row));
   GtkTreeIter iter;
   get_iter (row, &iter);
   row_changed (&iter);
@@ -772,6 +773,7 @@
                                    int   child_lo,
                                    int   child_hi)
 {
+  g_assert (row_is_in_tree (parent));
   const int n_children (parent->n_children());
   child_hi = std::min (child_hi, n_children);
 
@@ -799,7 +801,8 @@
 void
 PanTreeStore :: insert_sorted (const parent_to_children_t& new_parents)
 {
-  g_return_if_fail (!new_parents.empty());
+  g_assert (the_kids_are_alright (root));
+  g_assert (!new_parents.empty());
 
   RowCompareByDepth depth_compare (this);
 
@@ -832,6 +835,7 @@
       pool.erase (parent);
     }
   }
+  g_assert (the_kids_are_alright (root));
 }
 
 struct PanTreeStore :: RowCompareByColumn
@@ -904,7 +908,9 @@
 PanTreeStore :: insert_sorted (Row           * parent,
                                const rows_t  & children_in)
 {
-  g_return_if_fail (!children_in.empty());
+  g_assert (row_is_in_tree (parent));
+  g_assert (the_kids_are_alright (root));
+  g_assert (!children_in.empty());
 
   if (!parent)
        parent = root;
@@ -1002,6 +1008,8 @@
 
   //for (int i=0, n=parent->children.size(); i<n; ++i)
    // g_assert (i==0 || !compare_column(parent->children[i], parent->children[i-1]));
+
+  g_assert (the_kids_are_alright (root));
 }
 
 void
@@ -1009,8 +1017,10 @@
                         const rows_t   & new_children,
                         int              position)
 {
-  g_return_if_fail (!new_children.empty());
-  g_return_if_fail (position >= 0);
+  g_assert (!parent_row || row_is_in_tree (parent_row));
+  g_assert (the_kids_are_alright (root));
+  g_assert (!new_children.empty());
+  g_assert (position >= 0);
 
   if (!parent_row)
        parent_row = root;
@@ -1057,6 +1067,7 @@
 
   // cleanup
   gtk_tree_path_free (path);
+  g_assert (the_kids_are_alright (root));
 }
 
 void
@@ -1064,7 +1075,10 @@
                           Row  * row,
                           int    position)
 {
-  g_return_if_fail (row != 0);
+  g_assert (row_is_in_tree (new_parent));
+  g_assert (row_is_in_tree (row));
+  g_assert (the_kids_are_alright (root));
+  g_assert (row != 0);
 
   GtkTreeModel * model (GTK_TREE_MODEL(this));
 
@@ -1102,11 +1116,15 @@
     gtk_tree_model_row_has_child_toggled (model, path, &new_parent_iter);
     gtk_tree_path_free (path);
   }
+
+  g_assert (the_kids_are_alright (root));
 }
 
 void
 PanTreeStore :: reparent (const parent_to_children_t& new_parents)
 {
+  g_assert (the_kids_are_alright (root));
+
   const RowCompareByDepth depth_compare (this);
 
   // REMOVE children from their OLD parents
@@ -1119,6 +1137,8 @@
   // ADD children to their NEW parent
 
   insert_sorted (new_parents);
+
+  g_assert (the_kids_are_alright (root));
 }
   
 /***
@@ -1128,6 +1148,7 @@
 void
 PanTreeStore :: remove (const rows_t& rows, bool delete_rows)
 {
+  g_assert (the_kids_are_alright (root));
   parent_to_children_t parent_to_kids;
   foreach_const (rows_t, rows, it) {
     g_assert (*it);
@@ -1144,6 +1165,7 @@
   std::sort (keys.begin(), keys.end(), compare_depth);
   foreach_const_r (rows_t, keys, it)
     remove_siblings (parent_to_kids[*it], delete_rows);
+  g_assert (the_kids_are_alright (root));
 }
 
 struct PanTreeStore::ClearWalker: public PanTreeStore::WalkFunctor
@@ -1179,14 +1201,38 @@
   }
 };
 
+bool
+PanTreeStore :: the_kids_are_alright (Row * parent)
+{
+  g_return_val_if_fail (row_is_in_tree (parent), false);
+  g_return_val_if_fail (parent, false);
+  for (int i=0, n=parent->n_children(); i<n; ++i) {
+    Row * child (parent->nth_child (i));
+    g_return_val_if_fail (child, false);
+    g_return_val_if_fail (child->parent == parent, false);
+    g_return_val_if_fail (child->child_index == i, false);
+    g_return_val_if_fail (the_kids_are_alright (child), false);
+  }
+  return true;
+}
+
+bool
+PanTreeStore :: row_is_in_tree (Row * row)
+{
+  return row && (row==root || get_depth(row)>=0);
+}
+
 void
 PanTreeStore :: remove_siblings (const rows_t& siblings,
                                  bool          delete_rows)
 {
   // entry assertions
+  g_assert (the_kids_are_alright (root));
   g_assert (!siblings.empty());
   Row * parent (siblings[0]->parent);
-  g_assert (parent);
+  g_assert (row_is_in_tree (parent));
+  const int old_n_children (parent->n_children());
+  g_assert ((int)siblings.size() <= old_n_children);
   foreach_const (rows_t, siblings, it)
     g_assert ((*it)->parent == parent); // all are siblings
 
@@ -1200,6 +1246,7 @@
     row->parent = 0;
     row->child_index = -1;
   }
+  g_assert (siblings.size() == removed_indices.size());
 
   // remove the dead rows; re-index the live ones
   int pos (0);
@@ -1216,6 +1263,7 @@
       postfix_walk (walk, &iter);
     }
   }
+  g_assert (siblings.size() == old_n_children - keepers.size());
   parent->children.swap (keepers);
 
   // fire removal signal for child
@@ -1232,6 +1280,7 @@
     gtk_tree_model_row_has_child_toggled (model, path, &pit);
   }
 
+  g_assert (the_kids_are_alright (root));
   gtk_tree_path_free (path);
 }
 
@@ -1245,7 +1294,6 @@
                              GtkTreePath * path,
                              WalkFunctor & walker)
 {
-  g_assert (row);
   g_assert (walk_mode==WALK_PREFIX || walk_mode==WALK_POSTFIX);
 
   bool more (true);
