From: Vincent Pit Date: Tue, 20 Jan 2009 22:32:44 +0000 (+0100) Subject: Fix refcount of scalars with free callback X-Git-Tag: v0.28~15 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=7c1660201c0aafbfb8842d97f6728b74005b286b;p=perl%2Fmodules%2FVariable-Magic.git Fix refcount of scalars with free callback Or they'll appear as leaked for non null destruction levels. --- diff --git a/Magic.xs b/Magic.xs index 40997ed..c0c4297 100644 --- a/Magic.xs +++ b/Magic.xs @@ -569,6 +569,7 @@ STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) { 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) @@ -586,7 +587,13 @@ STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) { /* 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