From 7e5d55b35d0c95380f436d0ebd75bab73fce8622 Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Mon, 17 Jun 2024 15:24:38 +1000 Subject: [PATCH] class.c: store the stash in the constructor CV stash slot 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. --- class.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/class.c b/class.c index ed7368136899..07bad10498c0 100644 --- a/class.c +++ b/class.c @@ -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); @@ -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: