develooper Front page | | Postings from July 2022

Re: Having memory leak issues with perl-c

Thread Previous
July 28, 2022 08:50
Re: Having memory leak issues with perl-c
Message ID:
On Thu, 28 Jul 2022 at 10:35, demerphq <> wrote:

> On Wed, 27 Jul 2022 at 22:41, Mark Murawski <>
> wrote:
>> On 7/27/22 13:20, demerphq wrote:
>> Spectacular.  I'll be reviewing and making changes that you're suggesting
>> and this helps my understanding for sure.
> I was a bit off my game yesterday. I should have told you to make liberal
> use of sv_dump() when you are debugging. That will help you visualize what
> is going on. It's the XS equivalent to the Dump function from Devel::Peek
> (or more accurately it is what the Dump functions uses under the hood).
> You can pass any SV like structure (Eg, HV, AV, etc) into it with casting
> and it will dump out all the useful details, including refcounts.
> Good luck!

Last comment. You might find the code in Sereal::Encoder and
Sereal::Decoder helpful. Sereal is a serialization package I maintain, and
as such it contains all the code for serializing perl data structures and
then reconstituting them. It was written in a style where it avoids mortal
values as much as possible, where it mortalizes the root of the data
structure being deserialized and then does not mortalize anything else
(barring state data), and passes down the variables being "filled in"
(usually called the SV *into). So for instance if it is deserializing an
array ref, it first creates an SV with refcount 1, it then passes that into
the code that parses array refs, which turns the into into an SvRV pointing
at a newAV, then for each element in the array it creates a newSV, pushes
it into the array, and then calls the parse logic with that new element as
the into argument, etc. Thus it bypasses all of the recount twiddling. If
the code dies in the middle the root is mortal so Perl will free it later.

A mortal pattern might look like this:

HV *sv= newSV(0);
sv_2mortal(sv); /* if we die this will get freed when perl cleans up later!

/* do stuff that could die */

/* Yay, we didn't die! increment sv's refcount to demortalize it and then
return it */
return sv;

Sorry, just trying to arm you with as much useful info as I can. I happened
to be working on very similar code the last day or two for Sereal. :-)


perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About