Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Attempt to free unreferenced scalar

by ikegami (Pope)
on Mar 16, 2013 at 18:55 UTC ( #1023845=note: print w/ replies, xml ) Need Help??


in reply to Attempt to free unreferenced scalar

Cpantesters reports that my distro NoSQL::PL2SQL is failing in v5.10 and v5.12.

Clearly

The errors on CPAN are probably due to the failure that tobyink documented earlier, Attempt to free unreferenced scalar in line 53.

Yeah, every failing 5.10 and 5.12 report I checked had this.

"Attempt to free unreferenced scalar" is caused by over-decrementing a refcount.

use strict; use warnings; use Inline C => <<'__EOI__'; void testing() { SV* sv = newSV(0); SvREFCNT_dec(sv); /* sv is freed */ SvREFCNT_dec(sv); /* Attempt to free unreferenced scalar */ } __EOI__ testing();

But I've looked at the extent of your use of XS... It's obviously not a bug in your module.

So it's either a bug in Perl or a bug in a dependency (with an XS component).

Do you have a minimal test case?


Comment on Re: Attempt to free unreferenced scalar
Download Code
Re^2: Attempt to free unreferenced scalar
by Anonymous Monk on Mar 19, 2013 at 17:33 UTC

    I spent about 1/2 hour trying to build a minimal test case. So far, I have not successfully isolated the problem.

    I made a few discoveries, though. According to perldiag, this message is considered a warning. True enough, even though the test script ultimately fails, it continues to run after displaying this warning message.

    Approximately 20% of the time when I run the test script, I get the following output:

    PERL_DL_NONLAZY=1 /home/jim/perl5.12/usr/bin/perl "-MExtUtils::Command +::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t t/NoSQL-PL2SQL.t .. ok t/sqlite.t ........ 25/59 Attempt to free unreferenced scalar: SV 0x8e +3b058 at /home/jim/.cpan/build/NoSQL-PL2SQL-1.20-5swcte/blib/lib/NoSQ +L/PL2SQL/Object.pm line 53, <H> chunk 1. *** glibc detected *** /home/jim/perl5.12/usr/bin/perl: free(): invali +d pointer: 0x08e3c8a0 *** ======= Backtrace: ========= /lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0x40110ee2] /home/jim/perl5.12/usr/bin/perl(Perl_mg_free+0x52)[0x80b5d22] /home/jim/perl5.12/usr/bin/perl(Perl_sv_clear+0x583)[0x80cfc43] /home/jim/perl5.12/usr/bin/perl(Perl_sv_free2+0x36)[0x80d0036] /home/jim/perl5.12/usr/bin/perl(Perl_sv_clear+0x648)[0x80cfd08] /home/jim/perl5.12/usr/bin/perl(Perl_sv_free2+0x36)[0x80d0036] /home/jim/perl5.12/usr/bin/perl(Perl_av_clear+0xd8)[0x80c13d8] /home/jim/perl5.12/usr/bin/perl(Perl_pp_aassign+0x1fb)[0x80c581b] /home/jim/perl5.12/usr/bin/perl(Perl_runops_standard+0x1d)[0x80c2c2d] /home/jim/perl5.12/usr/bin/perl(perl_run+0x2af)[0x80771cf] /home/jim/perl5.12/usr/bin/perl(main+0x10d)[0x805f79d] /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x400b44d3] /home/jim/perl5.12/usr/bin/perl[0x805f7d1] ======= Memory map: ======== 08048000-0816f000 r-xp 00000000 08:06 12070401 /home/jim/perl5.12/us +r/bin/perl 0816f000-08170000 r--p 00126000 08:06 12070401 /home/jim/perl5.12/us +r/bin/perl 08170000-08172000 rw-p 00127000 08:06 12070401 /home/jim/perl5.12/us +r/bin/perl 08172000-08173000 rw-p 00000000 00:00 0 088cd000-08ee6000 rw-p 00000000 00:00 0 [heap] 40000000-40020000 r-xp 00000000 08:06 919427 /lib/i386-linux-gnu/l +d-2.15.so 40020000-40021000 r--p 0001f000 08:06 919427 /lib/i386-linux-gnu/l +d-2.15.so 40021000-40022000 rw-p 00020000 08:06 919427 /lib/i386-linux-gnu/l +d-2.15.so 40022000-40023000 r-xp 00000000 00:00 0 [vdso] 40023000-40025000 rw-p 00000000 00:00 0 40025000-40026000 r--p 005e0000 08:06 12592234 /usr/lib/locale/local +e-archive 40026000-4002b000 r-xp 00000000 08:06 3680652 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/List/Util/Util.so 4002b000-4002c000 r--p 00004000 08:06 3680652 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/List/Util/Util.so 4002c000-4002d000 rw-p 00005000 08:06 3680652 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/List/Util/Util.so 4002d000-40031000 r-xp 00000000 08:06 3811631 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/IO/IO.so 40031000-40032000 r--p 00003000 08:06 3811631 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/IO/IO.so 40032000-40033000 rw-p 00004000 08:06 3811631 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/IO/IO.so 40033000-40035000 r-xp 00000000 08:06 3680638 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Fcntl/Fcntl.so 40035000-40036000 ---p 00002000 08:06 3680638 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Fcntl/Fcntl.so 40036000-40037000 r--p 00002000 08:06 3680638 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Fcntl/Fcntl.so 40037000-40038000 rw-p 00003000 08:06 3680638 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Fcntl/Fcntl.so 40038000-4003b000 r-xp 00000000 08:06 920376 /lib/i386-linux-gnu/l +ibdl-2.15.so 4003b000-4003c000 r--p 00002000 08:06 920376 /lib/i386-linux-gnu/l +ibdl-2.15.so 4003c000-4003d000 rw-p 00003000 08:06 920376 /lib/i386-linux-gnu/l +ibdl-2.15.so 4003d000-40067000 r-xp 00000000 08:06 919426 /lib/i386-linux-gnu/l +ibm-2.15.so 40067000-40068000 r--p 00029000 08:06 919426 /lib/i386-linux-gnu/l +ibm-2.15.so 40068000-40069000 rw-p 0002a000 08:06 919426 /lib/i386-linux-gnu/l +ibm-2.15.so 40069000-4006a000 rw-p 00000000 00:00 0 4006a000-40072000 r-xp 00000000 08:06 919416 /lib/i386-linux-gnu/l +ibcrypt-2.15.so 40072000-40073000 r--p 00007000 08:06 919416 /lib/i386-linux-gnu/l +ibcrypt-2.15.so 40073000-40074000 rw-p 00008000 08:06 919416 /lib/i386-linux-gnu/l +ibcrypt-2.15.so 40074000-4009b000 rw-p 00000000 00:00 0 4009b000-4023e000 r-xp 00000000 08:06 919415 /lib/i386-linux-gnu/l +ibc-2.15.so 4023e000-4023f000 ---p 001a3000 08:06 919415 /lib/i386-linux-gnu/l +ibc-2.15.so 4023f000-40241000 r--p 001a3000 08:06 919415 /lib/i386-linux-gnu/l +ibc-2.15.so 40241000-40242000 rw-p 001a5000 08:06 919415 /lib/i386-linux-gnu/l +ibc-2.15.so 40242000-40246000 rw-p 00000000 00:00 0 40246000-40446000 r--p 00000000 08:06 12592234 /usr/lib/locale/local +e-archive 40446000-40455000 r-xp 00000000 08:06 410938 /home/jim/perl5/lib/p +erl5/i686-linux/auto/XML/Parser/Expat/Expat.so 40455000-40456000 r--p 0000f000 08:06 410938 /home/jim/perl5/lib/p +erl5/i686-linux/auto/XML/Parser/Expat/Expat.so 40456000-40457000 rw-p 00010000 08:06 410938 /home/jim/perl5/lib/p +erl5/i686-linux/auto/XML/Parser/Expat/Expat.so 40457000-4047d000 r-xp 00000000 08:06 920205 /lib/i386-linux-gnu/l +ibexpat.so.1.5.2 4047d000-4047e000 ---p 00026000 08:06 920205 /lib/i386-linux-gnu/l +ibexpat.so.1.5.2 4047e000-40480000 r--p 00026000 08:06 920205 /lib/i386-linux-gnu/l +ibexpat.so.1.5.2 40480000-40481000 rw-p 00028000 08:06 920205 /lib/i386-linux-gnu/l +ibexpat.so.1.5.2 40481000-40485000 r-xp 00000000 08:06 3811529 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/File/Glob/Glob.so 40485000-40486000 r--p 00003000 08:06 3811529 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/File/Glob/Glob.so 40486000-40487000 rw-p 00004000 08:06 3811529 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/File/Glob/Glob.so 40487000-404a5000 r-xp 00000000 08:06 3811824 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Compress/Raw/Zlib/Zlib.so 404a5000-404a6000 r--p 0001d000 08:06 3811824 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Compress/Raw/Zlib/Zlib.so 404a6000-404a7000 rw-p 0001e000 08:06 3811824 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Compress/Raw/Zlib/Zlib.so 404a7000-404a9000 r-xp 00000000 08:06 12583135 /home/jim/.cpan/build +/NoSQL-PL2SQL-1.20-5swcte/blib/arch/auto/NoSQL/PL2SQL/PL2SQL.so 404a9000-404aa000 r--p 00001000 08:06 12583135 /home/jim/.cpan/build +/NoSQL-PL2SQL-1.20-5swcte/blib/arch/auto/NoSQL/PL2SQL/PL2SQL.so 404aa000-404ab000 rw-p 00002000 08:06 12583135 /home/jim/.cpan/build +/NoSQL-PL2SQL-1.20-5swcte/blib/arch/auto/NoSQL/PL2SQL/PL2SQL.so 404ab000-404c8000 r-xp 00000000 08:06 13501138 /home/jim/perl5/lib/p +erl5/i686-linux/auto/DBI/DBI.so 404c8000-404c9000 r--p 0001c000 08:06 13501138 /home/jim/perl5/lib/p +erl5/i686-linux/auto/DBI/DBI.so 404c9000-404ca000 rw-p 0001d000 08:06 13501138 /home/jim/perl5/lib/p +erl5/i686-linux/auto/DBI/DBI.so 404ca000-404db000 r-xp 00000000 08:06 3680672 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Storable/Storable.so 404db000-404dc000 r--p 00010000 08:06 3680672 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Storable/Storable.so 404dc000-404dd000 rw-p 00011000 08:06 3680672 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Storable/Storable.so 404dd000-404e1000 r-xp 00000000 08:06 3811512 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Digest/MD5/MD5.so 404e1000-404e2000 r--p 00003000 08:06 3811512 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Digest/MD5/MD5.so 404e2000-404e3000 rw-p 00004000 08:06 3811512 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Digest/MD5/MD5.so 404e3000-404ea000 r-xp 00000000 08:06 3680599 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Data/Dumper/Dumper.so 404ea000-404eb000 r--p 00007000 08:06 3680599 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Data/Dumper/Dumper.so 404eb000-404ec000 rw-p 00008000 08:06 3680599 /usr/local/lib/perl5/ +5.12.5/i686-linux/auto/Data/Dumper/Dumper.so 404ec000-405a9000 r-xp 00000000 08:06 411295 /home/jim/perl5/lib/p +erl5/i686-linux/auto/DBD/SQLite/SQLite.so 405a9000-405aa000 ---p 000bd000 08:06 411295 /home/jim/perl5/lib/p +erl5/i686-linux/auto/DBD/SQLite/SQLite.so 405aa000-405ab000 r--p 000bd000 08:06 411295 /home/jim/perl5/lib/p +erl5/i686-linux/auto/DBD/SQLite/SQLite.so 405ab000-405ac000 rw-p 000be000 08:06 411295 /home/jim/perl5/lib/p +erl5/i686-linux/auto/DBD/SQLite/SQLite.so 405ac000-405ae000 rw-p 00000000 00:00 0 405c1000-405dd000 r-xp 00000000 08:06 920239 /lib/i386-linux-gnu/l +ibgcc_s.so.1 405dd000-405de000 r--p 0001b000 08:06 920239 /lib/i386-linux-gnu/l +ibgcc_s.so.1 405de000-405df000 rw-p 0001c000 08:06 920239 /lib/i386-linux-gnu/l +ibgcc_s.so.1 bfe45000-bfe66000 rw-p 00000000 00:00 0 [stack] t/sqlite.t ........ Failed 15/59 subtests Test Summary Report ------------------- t/sqlite.t (Wstat: 134 Tests: 44 Failed: 0) Non-zero wait status: 134 Parse errors: Bad plan. You planned 59 tests but ran 44. Files=2, Tests=50, 1 wallclock secs ( 0.07 usr 0.01 sys + 0.81 cusr + 0.04 csys = 0.93 CPU) Result: FAIL Failed 1/2 test programs. 0/50 subtests failed. make: *** [test_dynamic] Error 255

    My current theory is that the free() call is failing even when I don't see any output. Is that assumption reasonable?

    Mind you, the code in question is called during destruction. The code is intended to process unreferenced scalars that have already been destroyed. In Perl versions where this distro is successful, these scalars have an undef value that is easily reassigned.

    In the case of errors and warnings, the warning seems to be initiated as soon as the scalar is assigned with another reference.

    Hopefully, with more poking and prodding, I'll have more observations to share soon.

    Thanks!

    Jim

      According to perldiag, this message is considered a warning.

      You shouldn't be dismissing the severity. The code is accessing a freed memory!

      My current theory is that the free() call is failing even when I don't see any output. Is that assumption reasonable?

      Accessing freed memory can lead to a wide variety of behaviour. It's a waste of time looking at this when you already know what leads to this memory corruption (accessing a freed scalar).

      So far, I have not successfully isolated the problem.

      An alternate approach would be to find the commit that fixed the problem. This might give a hint how to work around the problem in the versions before it was fixed.

      There are tools to bisect history to find what a problem started occurring, or in this case, stopped occurring. I don't know how easy it would be to automatically install modules as part of the process.

        Thanks again for the helpful response.

        My understanding is that Perl will not try to free() a weak reference. But I seem to recall reading that a weak reference is copied as a strong reference. If true, that might explain everything, since the copy will trigger free() on memory already unallocated.

        I'm probably out of my league, but I assume that a weak reference copy should also be weak. Is this behavior limited to 5.10 and 5.12? Certainly a neat and tidy explanation and a manageable workaround. You're probably the best resource for sanity checking that theory. And then, is it possible to confirm by rooting around the change logs?

        It's always a exciting... and a little scary... to get so close to the internals ;)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1023845]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2014-08-28 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (257 votes), past polls