Index: trunk/src/libcschem/plug_library.c =================================================================== --- trunk/src/libcschem/plug_library.c (revision 10355) +++ trunk/src/libcschem/plug_library.c (revision 10356) @@ -324,10 +324,22 @@ return 0; } +/* Replace orig_root with new_root in a sheet's lib */ +static void lib_rehash_update_root_in_all_sheets(csch_sheet_t *sheet, csch_lib_master_t *master, csch_lib_t *orig_root, csch_lib_t *new_root) +{ + long n; + csch_lib_root_t *libroot = sheet->libs.array[master->uid]; + + assert(libroot != NULL); /* csch_lib_add() created it */ + for(n = 0; n < libroot->roots.used; n++) + if (libroot->roots.array[n] == orig_root) + libroot->roots.array[n] = new_root; +} + int csch_lib_rehash(csch_sheet_t *sheet, csch_lib_master_t *master, csch_lib_t *node) { - csch_lib_root_t *libroot; csch_lib_t *root = node, *orig_root, *new_root; + rnd_design_t *dsg; long n; while(root->parent != NULL) root = root->parent; @@ -352,13 +364,11 @@ assert(new_root != NULL); /* csch_lib_add() created it */ TODO("TODO#41: sharing root pointers may not be a good idea: they will not have unique names; see the other TODO#41"); - /* have to fix up aliases */ - libroot = sheet->libs.array[master->uid]; - assert(libroot != NULL); /* csch_lib_add() created it */ - for(n = 0; n < libroot->roots.used; n++) { - if (libroot->roots.array[n] == orig_root) - libroot->roots.array[n] = new_root; - } + + /* have to fix up aliases - in all open sheets because of shared roots! */ + for(dsg = gdl_first(&rnd_designs); dsg != NULL; dsg = dsg->link.next) + lib_rehash_update_root_in_all_sheets((csch_sheet_t *)dsg, master, orig_root, new_root); + csch_lib_free(orig_root); return 0; }