Why does the following XS code leak? #include "EXTERN.h" #include "perl.h" #include "XSUB.h"
typedef struct { char *buf; } Foo; Foo *FooAlloc(pTHX) { Foo *foo; New(0, foo, 1, Foo); New(0, foo->buf, 1024, char); return foo; } void FooFree(pTHX_ Foo *foo) { Safefree(foo->buf); //Safefree(foo); } MODULE = Foo PACKAGE = Foo void foo() PPCODE: { SV *sv; Foo *foo; sv = NEWSV(0, 0); foo = FooAlloc(aTHX); sv_usepvn(sv, (char *)foo, sizeof *foo); SvPOK_only(sv); SvREADONLY_on(sv); FooFree(aTHX_ foo); SvREFCNT_dec(sv); } The Safefree(foo) is commented out in FooFree() because I believe that sv_usepvn() has reallo()'ed foo and will free() it for me when sv's refcount becomes 0. I tried uncommenting the Safefree(foo) to see if it made any difference, but it still leaks. The leak is easily seen just by building a Foo module out of the above and running perl -Mblib -MFoo -e "<STDIN>; for (1 ..10000) { Foo::foo() } <STDIN>;" If I comment-out the sv_usepvn() call, and accordingly restore the Safefree(foo) once more, then the XSUB doesn't leak. - Steve ------------------------------------------------ Radan Computational Ltd. The information contained in this message and any files transmitted with it are confidential and intended for the addressee(s) only. If you have received this message in error or there are any problems, please notify the sender immediately. The unauthorized use, disclosure, copying or alteration of this message is strictly forbidden. Note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of Radan Computational Ltd. The recipient(s) of this message should check it and any attached files for viruses: Radan Computational will accept no liability for any damage caused by any virus transmitted by this email.