#define VMG_SAVE_LAST_CX (!VMG_HAS_PERL(5, 8, 4) || VMG_HAS_PERL(5, 9, 5))
-STATIC I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, I32 destructor) {
+STATIC I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, SV *dsv) {
#define vmg_call_sv(S, F, D) vmg_call_sv(aTHX_ (S), (F), (D))
I32 ret, cxix = 0, in_eval = 0;
#if VMG_SAVE_LAST_CX
if (cxstack_ix < cxstack_max) {
cxix = cxstack_ix + 1;
- if (destructor && CxTYPE(cxstack + cxix) == CXt_EVAL)
+ if (dsv && CxTYPE(cxstack + cxix) == CXt_EVAL)
in_eval = 1;
}
#else
++PL_Ierror_count;
#endif
- } else if (!in_eval)
+ } else if (!in_eval) {
+ if (dsv) {
+ /* We are about to croak() while dsv is being destroyed. Try to clean up
+ * things a bit. */
+ MAGIC *mg = SvMAGIC(dsv);
+ SvREFCNT_dec((SV *) mg->mg_ptr);
+ /* mg->mg_obj may not be refcounted if the data constructor returned the
+ * variable itself. */
+ if (mg->mg_flags & MGf_REFCOUNTED)
+ SvREFCNT_dec(mg->mg_obj);
+ SvMAGIC_set(dsv, mg->mg_moremagic);
+ Safefree(mg);
+ mg_magical(dsv);
+ SvREFCNT_dec(dsv);
+ }
croak(NULL);
+ }
} else {
if (old_err) {
SvREFCNT_dec(ERRSV);
PUSHs(args[i]);
PUTBACK;
- vmg_call_sv(ctor, G_SCALAR, 0);
+ vmg_call_sv(ctor, G_SCALAR, NULL);
SPAGAIN;
nsv = POPs;
XPUSHs(vmg_op_info(opinfo));
PUTBACK;
- vmg_call_sv(cb, G_SCALAR, 0);
+ vmg_call_sv(cb, G_SCALAR, NULL);
SPAGAIN;
svr = POPs;
XPUSHs(vmg_op_info(opinfo));
PUTBACK;
- vmg_call_sv(w->cb_len, G_SCALAR, 0);
+ vmg_call_sv(w->cb_len, G_SCALAR, NULL);
SPAGAIN;
svr = POPs;
XPUSHs(vmg_op_info(w->opinfo));
PUTBACK;
- vmg_call_sv(w->cb_free, G_SCALAR, 1);
+ vmg_call_sv(w->cb_free, G_SCALAR, sv);
SPAGAIN;
svr = POPs;