Index: generic/nsf.c =================================================================== diff -u -rd710110cf4f4425bbeb8313d335d68a8c7bd3ba9 -r49900e2a66b1bd8cb5ff9ec6f16d9cb80ce28656 --- generic/nsf.c (.../nsf.c) (revision d710110cf4f4425bbeb8313d335d68a8c7bd3ba9) +++ generic/nsf.c (.../nsf.c) (revision 49900e2a66b1bd8cb5ff9ec6f16d9cb80ce28656) @@ -25181,7 +25181,7 @@ } #endif - /*fprintf(stderr, "+++ call tcl-defined exit handler\n");*/ + /*fprintf(stderr, "+++ call tcl-defined exit handler (%x)\n", PTR2INT(pthread_self()));*/ /* * Evaluate user-defined exit handler. @@ -31577,7 +31577,19 @@ continue; } - /*fprintf(stderr, "cl key = %s %p\n", ClassName(cl), cl); */ + /*fprintf(stderr, "### cl key = %s %p\n", ClassName(cl), cl); */ + + /* + * Remove manually mixinRegObjs to achieve correct deletion + * order. Otherwise, refcount checking for NsfObjects complains during + * shutdown (and dangling references would be a consequence). + */ + if (cl->opt != NULL && cl->opt->mixinRegObjs != NULL) { + NsfMixinregInvalidate(interp, cl->opt->mixinRegObjs); + DECR_REF_COUNT2("mixinRegObjs", cl->opt->mixinRegObjs); + cl->opt->mixinRegObjs = NULL; + } + if (!ObjectHasChildren((NsfObject *)cl) && !ClassHasInstances(cl) && !ClassHasSubclasses(cl)