Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Regex replace leaking memory.

by dave_the_m (Monsignor)
on Aug 25, 2020 at 08:12 UTC ( #11121071=note: print w/replies, xml ) Need Help??

in reply to Regex replace leaking memory.

When perl does a match, it takes a copy of the original string (and a list of indices of where $1, $& etc matched) so that if $1 or $& etc are used later within the current scope, their value can be constructed on demand (the $1 etc variables are effectively tied). This extra SV is stored along with the regex object. I suspect in this case Devel::Leak is wrong.

Note that the copy uses copy-on-write, which allows multiple string SVs to share the same string buffer. In your case, the constant string "TESTING STRING" is shared with $string when the latter is assigned to. When the match is done, the string becomes shared for a second time. When the substitution is then done, $string is modified and its string buffer becomes unshared. This leaves the original buffer shared by the constant SV and the SV in the regex. I think the regex's SV is the one being displayed by Devel::Leak.

Over various releases, perl has differed in how it manages the copy kept by the regex; in particular, when the pattern itself had no captures and $& hadn't yet been seen during compilation perl used to overly-optimise by not capturing, and eval '$&' could return garbage.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11121071]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2020-10-20 01:03 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (208 votes). Check out past polls.