Skip to content

Add SAVEt_PADSV; fixes memory leak in pp_methstart#24254

Merged
leonerd merged 1 commit intoPerl:bleadfrom
leonerd:fix-memoryleak-in-pr24211
Mar 9, 2026
Merged

Add SAVEt_PADSV; fixes memory leak in pp_methstart#24254
leonerd merged 1 commit intoPerl:bleadfrom
leonerd:fix-memoryleak-in-pr24211

Conversation

@leonerd
Copy link
Copy Markdown
Contributor

@leonerd leonerd commented Mar 4, 2026

The previous commit here (6b45044) intended to fix pp_methstart to allow refaliasing operations on fields. The way it did this had the unintended side-effect of no longer undoing the SvREFCNT_inc applied to each field, and thus caused every method call to leak SV references to every field it captured.

This change fixes this, by introducing a new savestack operation, SAVEt_PADSV. This operation saves the current SV in a pad slot, then on scope exit restores it back by first decrementing the reference count of whatever SV is now found in that pad slot. This was the original bug in pp_methstart that refaliasing into fields highlighted and the previous commit intended to fix.

In addition to the bugfix, this new savestack operation makes it more efficient than the previous code, as it combines into a single operation the two behaviours which previously required two separate ones (SAVEt_SPTR + SAVEt_CLEARSV).

  • This set of changes does not require a perldelta entry, but only because the previous bugfix didn't appear in perldelta either.

Comment thread t/class/field.t Outdated
require Config;

eval { require XS::APItest; XS::APItest->import('sv_count'); 1 }
or skip_all("XS::APItest not available");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The downside to this is that this entire test file won't run now if XS::APItest can't be imported. Is that okay? (When is that even possible??)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already quite a few tests in t/op/ that do similar things; e.g. grep for "XS::APItest not available". I think we would be in good company this way.

Copy link
Copy Markdown
Contributor Author

@leonerd leonerd Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that said some of them just make one test block conditional. I'll have a look at doing that.

Edit: Done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(When is that even possible??)

Primarily when doing a minitest

@leonerd leonerd force-pushed the fix-memoryleak-in-pr24211 branch 2 times, most recently from 32a1652 to a4ce088 Compare March 4, 2026 23:01
Comment thread sv.c Outdated
Comment thread scope.c
@tonycoz
Copy link
Copy Markdown
Contributor

tonycoz commented Mar 4, 2026

Otherwise looks fine.

The previous commit here (6b45044) intended to fix `pp_methstart` to
allow refaliasing operations on fields. The way it did this had the
unintended side-effect of no longer undoing the `SvREFCNT_inc` applied
to each field, and thus caused every method call to leak SV references
to every field it captured.

This change fixes this, by introducing a new savestack operation,
`SAVEt_PADSV`. This operation saves the current SV in a pad slot, then
on scope exit restores it back by first decrementing the reference count
of *whatever SV* is now found in that pad slot. This was the original
bug in `pp_methstart` that refaliasing into fields highlighted and the
previous commit intended to fix.

In addition to the bugfix, this new savestack operation makes it more
efficient than the previous code, as it combines into a single operation
the two behaviours which previously required two separate ones
(SAVEt_SPTR + SAVEt_CLEARSV).
@leonerd leonerd force-pushed the fix-memoryleak-in-pr24211 branch from a4ce088 to 0140fdf Compare March 5, 2026 10:11
@leonerd
Copy link
Copy Markdown
Contributor Author

leonerd commented Mar 5, 2026

I believe that push fixes the issues Tony raises. I'll leave this a day or so for any other comments.
As this fixes a big memory leak bug that would affect any object class at all, I feel we do want to aim to get this in before the .10 freeze, so if nobody else comments by Monday 9th, I'll merge it.

@jkeenan
Copy link
Copy Markdown
Contributor

jkeenan commented Mar 6, 2026

Do you know of any CPAN distributions which use the class functionality or which might have some other reason to warrant testing against this p.r.?

@leonerd leonerd merged commit 3ed2439 into Perl:blead Mar 9, 2026
33 checks passed
@leonerd leonerd deleted the fix-memoryleak-in-pr24211 branch March 9, 2026 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants