On Wed, May 26, 2004 at 10:55:09PM -0700 Bill Moseley wrote: > On Thu, May 27, 2004 at 07:12:58AM +0200, Tassilo von Parseval wrote: > > > For XS modules, I'd always use valgrind (http://valgrind.kde.org/): > > Ok. I thought there might be a Perl module to check for leaks. But > valgrind is cool.
There's Devel::Leak. Before you install it, you should remove #ifndef DEBUGGING #define sv_dump(sv) PerlIO_printf(PerlIO_stderr(), "\n") #endif in Leak.xs as this is tailored towards older perls where sv_dump() only existed when DEBUGGING was set. > So, this shows leaks -- are those my errors or are they in Perl? > I'm guessing Perl because: > > valgrind --leak-check=yes perl -le "$x = 1" > > shows lost memory. But, I'm not so sure -- if I comment out my > "safefree()" call in my .xs code the report changes only slightly. Perl doesn't free all memory when shutting down. Usually there's no need for it since on exit the OS will do the clean up. You can set the environment variable PERL_DESTRUCT_LEVEL to 2 if you want everything to be freed and thus reducing false alarms as reported by valgrind. You might need to have a perl with -DDEBUGGING for that, though, not sure. As for non-reported obvious memory leaks, I also have some difficulties in getting a reported memory leak with SVs. I don't know why they are not reported. > Maybe it's better to wrap my tests in while(1) loop and just see if the > process size keeps growing! That's the scientific approach, eh? ;-) > Here's running my test script for my module (with the safefree() call > enabled). > > [EMAIL PROTECTED]:~/swish-e/perl$ valgrind --leak-check=yes perl -Iblib/lib > -Iblib/arch t/test.t > ==17816== Memcheck, a memory error detector for x86-linux. > ==17816== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward. > ==17816== Using valgrind-2.1.1, a program supervision framework for x86-linux. > ==17816== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward. > ==17816== For more details, rerun with: -v > ==17816== > 1..145 > ok 1 Call SWISH::API::new > > [...] > > ==17816== warning: Valgrind's siglongjmp is incomplete > ==17816== (it ignores cleanup handlers) > ==17816== your program may misbehave as a result > ==17816== > ==17816== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 26 from 2) > ==17816== malloc/free: in use at exit: 635283 bytes in 9153 blocks. > ==17816== malloc/free: 21118 allocs, 11965 frees, 18973223 bytes allocated. > ==17816== For counts of detected errors, rerun with: -v > ==17816== searching for pointers to 9153 not-freed blocks. > ==17816== checked 3813540 bytes. > ==17816== > ==17816== 5 bytes in 1 blocks are definitely lost in loss record 1 of 19 > ==17816== at 0x3C01F40D: malloc (vg_replace_malloc.c:105) > ==17816== by 0x80AAF25: Perl_savesharedpv (in /usr/bin/perl) > ==17816== by 0x8065994: (within /usr/bin/perl) > ==17816== by 0x80623C4: (within /usr/bin/perl) > ==17816== > ==17816== > ==17816== 200 bytes in 1 blocks are definitely lost in loss record 11 of 19 > ==17816== at 0x3C01F40D: malloc (vg_replace_malloc.c:105) > ==17816== by 0x3C05EF89: my_malloc (vg_libpthread.c:334) > ==17816== by 0x3C0621F0: get_or_allocate_specifics_ptr (vg_libpthread.c:1591) > ==17816== by 0x3C0623A4: pthread_key_create (vg_libpthread.c:1628) > ==17816== > ==17816== > ==17816== 1707 bytes in 13 blocks are definitely lost in loss record 13 of 19 > ==17816== at 0x3C01F40D: malloc (vg_replace_malloc.c:105) > ==17816== by 0x80A9FAE: Perl_safesysmalloc (in /usr/bin/perl) > ==17816== by 0x8066C64: Perl_init_stacks (in /usr/bin/perl) > ==17816== by 0x8060899: perl_construct (in /usr/bin/perl) > ==17816== > ==17816== > ==17816== 6352 bytes in 2 blocks are definitely lost in loss record 15 of 19 > ==17816== at 0x3C01FD01: realloc (vg_replace_malloc.c:162) > ==17816== by 0x80AA07F: Perl_safesysrealloc (in /usr/bin/perl) > ==17816== by 0x80E6059: Perl_savestack_grow (in /usr/bin/perl) > ==17816== by 0x80E6B27: Perl_save_I32 (in /usr/bin/perl) > ==17816== > ==17816== > ==17816== 143700 bytes in 13 blocks are possibly lost in loss record 18 of 19 > ==17816== at 0x3C01F40D: malloc (vg_replace_malloc.c:105) > ==17816== by 0x80A9FAE: Perl_safesysmalloc (in /usr/bin/perl) > ==17816== by 0x80B4D66: Perl_reentrant_init (in /usr/bin/perl) > ==17816== by 0x8060AB4: perl_construct (in /usr/bin/perl) In these cases it is obvious that the leaks are not caused by your code. If they were, you'd see something like ==1389== 900 bytes in 9 blocks are definitely lost in loss record 9 of 13 ==1389== at 0x4003ED9C: malloc (vg_clientfuncs.c:100) ==1389== by 0x808783B: Perl_safemalloc (in /usr/opt/perl5.5.3/bin/perl5.00503) ==1389== by 0x40239607: XS_Leak_leak (YourModule.xs:16) ==1389== by 0x8093EAC: Perl_pp_entersub (in /usr/opt/perl5.5.3/bin/perl5.00503) Tassilo -- $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({ pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#; $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval