STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
SV *wiz = (SV *) mg->mg_ptr;
+ int ret = 0;
/* This may happen in global destruction */
if (SvTYPE(wiz) == SVTYPEMASK)
/* Perl_mg_free will get rid of the magic and decrement mg->mg_obj and
* mg->mg_ptr reference count */
- return vmg_cb_call1e(SV2MGWIZ(wiz)->cb_free, sv, mg->mg_obj);
+ ret = vmg_cb_call1e(SV2MGWIZ(wiz)->cb_free, sv, mg->mg_obj);
+
+ /* Calling SvREFCNT_dec() will trigger destructors in an infinite loop, so
+ * we have to rely on SvREFCNT() being a lvalue. Heck, even the core does it */
+ --SvREFCNT(sv);
+
+ return ret;
}
#if MGf_COPY