From: Marcus Holland-Moritz Date: Wed, 18 Feb 2004 08:53:03 +0000 (+0000) Subject: 1. Add section to perlxs.pod describing that the refcount of AVs/HVs X-Git-Tag: accepted/trunk/20130322.191538~22333 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c4e79b56de248a67c4a29293bd16f39465dde417;p=platform%2Fupstream%2Fperl.git 1. Add section to perlxs.pod describing that the refcount of AVs/HVs returned from XSUBs through RETVAL isn't decremented as it is for SVs. This causes those XSUBs to leak memory and cannot be fixed without breaking existing CPAN modules that work around this bug. 2. Fix a memory leak of that kind in POSIX::localconv. p4raw-id: //depot/perl@22330 --- diff --git a/ext/POSIX/POSIX.xs b/ext/POSIX/POSIX.xs index deefbd1..8a0188d 100644 --- a/ext/POSIX/POSIX.xs +++ b/ext/POSIX/POSIX.xs @@ -1013,6 +1013,7 @@ localeconv() #ifdef HAS_LOCALECONV struct lconv *lcbuf; RETVAL = newHV(); + sv_2mortal((SV*)RETVAL); if ((lcbuf = localeconv())) { /* the strings */ if (lcbuf->decimal_point && *lcbuf->decimal_point) diff --git a/pod/perlxs.pod b/pod/perlxs.pod index f126ff8..c09947d 100644 --- a/pod/perlxs.pod +++ b/pod/perlxs.pod @@ -276,6 +276,63 @@ some heuristic code which tries to disambiguate between "truly-void" and "old-practice-declared-as-void" functions. Hence your code is at mercy of this heuristics unless you use C as return value.) +=head2 Returning SVs, AVs and HVs through RETVAL + +When you're using RETVAL to return an C, there's some magic +going on behind the scenes that should be mentioned. When you're +manipulating the argument stack using the ST(x) macro, for example, +you usually have to pay special attention to reference counts. (For +more about reference counts, see L.) To make your life +easier, the typemap file automatically makes C mortal when +you're returning an C. Thus, the following two XSUBs are more +or less equivalent: + + void + alpha() + PPCODE: + ST(0) = newSVpv("Hello World",0); + sv_2mortal(ST(0)); + XSRETURN(1); + + SV * + beta() + CODE: + RETVAL = newSVpv("Hello World",0); + OUTPUT: + RETVAL + +This is quite useful as it usually improves readability. While +this works fine for an C, it's unfortunately not as easy +to have C or C as a return value. You I be +able to write: + + AV * + array() + CODE: + RETVAL = newAV(); + /* do something with RETVAL */ + OUTPUT: + RETVAL + +But due to an unfixable bug (fixing it would break lots of existing +CPAN modules) in the typemap file, the reference count of the C +is not properly decremented. Thus, the above XSUB would leak memory +whenever it is being called. The same problem exists for C. + +When you're returning an C or a C, you have make sure +their reference count is decremented by making the AV or HV mortal: + + AV * + array() + CODE: + RETVAL = newAV(); + sv_2mortal((SV*)RETVAL); + /* do something with RETVAL */ + OUTPUT: + RETVAL + +And also remember that you don't have to do this for an C. + =head2 The MODULE Keyword The MODULE keyword is used to start the XS code and to specify the package