Skip to content

Commit

Permalink
Fix "Attempt to free unreferenced scalar during global destruction"
Browse files Browse the repository at this point in the history
Arguments to Perl 6 methods or callables are passed as array reference.
We created the array, and created a reference to this array to pass it
on to Perl 6 code. After the call we decreased the refcount of the array
instead of this reference.

The reference then got leaked and would be collected by perl during
global deconstruction. The reference's dtor would then try to decrease
the refcount of the array it was referencing which already was
destroyed.

This is what Perl was detecting and the source of the error.
Fixed by decreasing the refcount of the reference instead of the array.
  • Loading branch information
niner committed Aug 9, 2015
1 parent db25ccf commit f018d4b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions p5helper.c
Expand Up @@ -576,9 +576,10 @@ XS(p5_call_p6_method) {
MAGIC * const mg = mg_find(obj_deref, '~');
_perl6_magic* const p6mg = (_perl6_magic*)(mg->mg_ptr);
SV *err = NULL;
SV * retval = p6mg->call_p6_method(p6mg->index, name_pv, GIMME_V == G_SCALAR, newRV_noinc((SV *) args), &err);
SV * const args_rv = newRV_noinc((SV *) args);
SV * retval = p6mg->call_p6_method(p6mg->index, name_pv, GIMME_V == G_SCALAR, args_rv, &err);
SPAGAIN; /* refresh local stack pointer, could have been modified by Perl 5 code called from Perl 6 */
SvREFCNT_dec(args);
SvREFCNT_dec(args_rv);
if (err) {
sv_2mortal(err);
croak_sv(err);
Expand Down Expand Up @@ -624,9 +625,10 @@ XS(p5_call_p6_callable) {
MAGIC * const mg = mg_find(obj_deref, '~');
_perl6_magic* const p6mg = (_perl6_magic*)(mg->mg_ptr);
SV *err = NULL;
SV * retval = p6mg->call_p6_callable(p6mg->index, newRV_noinc((SV *) args), &err);
SV * const args_rv = newRV_noinc((SV *) args);
SV * retval = p6mg->call_p6_callable(p6mg->index, args_rv, &err);
SPAGAIN; /* refresh local stack pointer, could have been modified by Perl 5 code called from Perl 6 */
SvREFCNT_dec(args);
SvREFCNT_dec(args_rv);
if (err) {
sv_2mortal(err);
croak_sv(err);
Expand Down

0 comments on commit f018d4b

Please sign in to comment.