Skip to content

Commit

Permalink
class.c: store the stash in the constructor CV stash slot
Browse files Browse the repository at this point in the history
This code previously stored the stash for the class in the CV's
any_cv slot, and marked that to be released when the CV is released.

But it does not bump the reference count of the stash that I can
see.

When I tried releasing the constructor SV while working on #22169
I found perl would crash, incrementing the stash reference count
prevents that, but leaves us with a reference loop.

But it turns out that CVs already have a slot to store their stash in
and an API that correctly handles reference counting for that slot.

So use CvSTASH_set()/CvSTASH() to manage the stash for "$class::new"
methods.
  • Loading branch information
tonycoz committed Jun 20, 2024
1 parent 86a9c18 commit 39dd28f
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ XS(injected_constructor)
{
dXSARGS;

HV *stash = (HV *)XSANY.any_sv;
HV *stash = CvSTASH(cv);
assert(HvSTASH_IS_CLASS(stash));

struct xpvhv_aux *aux = HvAUX(stash);
Expand Down Expand Up @@ -371,8 +371,7 @@ Perl_class_setup_stash(pTHX_ HV *stash)
SAVEFREESV(newname);

CV *newcv = newXS_flags(SvPV_nolen(newname), injected_constructor, __FILE__, NULL, nameflags);
CvXSUBANY(newcv).any_sv = (SV *)stash;
CvREFCOUNTED_ANYSV_on(newcv);
CvSTASH_set(newcv, stash);
}

/* TODO:
Expand Down

0 comments on commit 39dd28f

Please sign in to comment.