- /* We reach this point in dirty state when ptable_free() is called from the
- * atexit cleanup callback, and that the global table still holds a live
- * wizard. This happens before all the SV bodies are freed, so all the wizard
- * callbacks are still alive (as they are referenced by the undead wizard).
- * Hence it is safe to decrement their refcount. Later on, the wizard free
- * callback itself will trigger when the wizard body is reaped, but it will
- * be skipped as it guards against dirty state - which is good since nothing
- * has to be done anymore at that point. */
-
- SvREFCNT_dec(w->cb_data);
- SvREFCNT_dec(w->cb_get);
- SvREFCNT_dec(w->cb_set);
- SvREFCNT_dec(w->cb_len);
- SvREFCNT_dec(w->cb_clear);
- SvREFCNT_dec(w->cb_free);
- SvREFCNT_dec(w->cb_copy);
+ /* During global destruction, any of the callbacks may already have been
+ * freed, so we can't rely on still being able to access them. */
+ if (!PL_dirty) {
+ SvREFCNT_dec(w->cb_data);
+ SvREFCNT_dec(w->cb_get);
+ SvREFCNT_dec(w->cb_set);
+ SvREFCNT_dec(w->cb_len);
+ SvREFCNT_dec(w->cb_clear);
+ SvREFCNT_dec(w->cb_free);
+ SvREFCNT_dec(w->cb_copy);