develooper Front page | perl.qa | Postings from July 2022

Having memory leak issues with perl-c

Thread Next
From:
Mark Murawski
Date:
July 27, 2022 15:46
Subject:
Having memory leak issues with perl-c
Message ID:
2101aede-608c-9479-6278-cb41dffedc09@intellasoft.net
Hi All!

I'm working on a new feature for plperl in postgresql to populate some 
metadata in main::_FN for running plperl functions from postgres sql.

I've snipped out some extra code that's unrelated to focus on the issue 
at hand.   If the section labeled 'Leaking Section' is entirely 
commented out (and of course the related SvREFCNT_dec_current is 
commented as well), then there's no memory issue at all.  If I do use 
this section of code, something is not freed and I'm not sure what that 
is, since I'm very new to perl-c



/*
  * Decrement the refcount of the given SV within the active Perl 
interpreter
  *
  * This is handy because it reloads the active-interpreter pointer, saving
  * some notation in callers that switch the active interpreter.
  */
static inline void
SvREFCNT_dec_current(SV *sv)
{
     dTHX;

     SvREFCNT_dec(sv);
}

static SV  *
plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
{
     dTHX;
     dSP;

     HV                 *hv;                  // hash
     SV                 *FNsv;                // scalar reference to the 
hash
     SV                 *svFN;                // local reference to the 
hash?

     ENTER;
     SAVETMPS;

     /* Give functions some metadata about what's going on in $_FN 
(Similar to $_TD for triggers) */

     // Leaking Section {
     FNsv = get_sv("main::_FN", GV_ADD);
     save_item(FNsv);                        /* local $_FN */

     hv = newHV(); // create new hash
     hv_ksplit(hv, 12);                      /* pre-grow the hash */
     hv_store_string(hv, "name", cstr2sv(desc->proname));

     svFN = newRV_noinc((SV *) hv); // reference to the new hash
     sv_setsv(FNsv, svFN);
     // Leaking Section }

     // dostuff

     SvREFCNT_dec_current(hv);

     PUTBACK;
     FREETMPS;
     LEAVE;

     ...snip...
}


If anyone would like to see the full context, I've attached the entire 
file.  My additions are between the 'New..........' sections

My question is... the perl-c api docs do not make it clear for which 
allocations or accesses that you need to decrement the ref count.

Does 'hv' need a decrement?
Does 'FNsv' need a decrement?
Does 'svFN' need a decrement?

Given that: If the code for storing main::_FN is commented out 
completely -- there is no memory leak... I'm pretty sure this is the 
source of the leak.  And being that I don't understand when you're 
supposed to decrement... it's probably in this code that the issue lays.

Thanks!

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About