John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I got a message "Attempt to free unreferenced scalar: SV etc" as the first line of output.

The line mentioned in the message (error message? warning message? it doesn't really say how worried I should be) is a use, and it is shown below:

use v5.8; use strict; use warnings; use utf8; use charnames ':full'; package VM_List; my $LN= "\N{POUND SIGN}"; # used for List Name. use overload '""' => \&stringify; sub create { my ($class, $listref)= @_; bless $listref, $class; return $listref; } sub stringify { my $self= shift; return "$LN( " . join (' ', @$self) . ' )'; } return 1; # loaded OK
Any clues what this means?

I'm running Perl 5.8.3, Activestate build 809.

—John

Replies are listed 'Best First'.
Re: Attempt to free unreferenced scalar
by jweed (Chaplain) on Feb 22, 2004 at 06:19 UTC
    Well, the only problem I can see is your last line. Return is for subroutines, for modules you need only an expression that evaluates to one. Usually, we use just the number one. By itself.

    Once I deleted the return it worked fine on my machine.

    Update: seems I was wrong. see Mr. Muskrat's reply, below.



    Code is (almost) always untested.
    http://www.justicepoetic.net/
      I tried without the return keyword before posting, and the results were the same.

      A while back, someone pointed out that this return is allowed and more readable than a 1 hanging on the end of the file.

Re: Attempt to free unreferenced scalar
by ambrus (Abbot) on Feb 22, 2004 at 13:30 UTC

    Are you using threads in the program? This is actually a perl internal error which you should rarely see.

Re: Attempt to free unreferenced scalar
by adrianh (Chancellor) on Feb 22, 2004 at 21:27 UTC

    Compiles fine (once the bogus return was removed) on 5.8.3, Mac OS... don't have a win box to hand I'm afraid...

      From the discussion a while back on this forum, the return is no more bogus than any other return that's at the natural end anyway.

      Even without (I tried deleting it in case the bahavior changed in the latest build) the error is the same.

        From the discussion a while back on this forum, the return is no more bogus than any other return that's at the natural end anyway.

        Hmmmm. When I run:

        #! /usr/bin/perl return 1;

        perl tells me

        Can't return outside a subroutine at return.pl line 2

        So I guess the previous discussion was somewhat inaccurate.

        Apologies that this is not help in solving your original problem.

Re: Attempt to free unreferenced scalar
by meetraz (Hermit) on Feb 23, 2004 at 05:07 UTC
    I assume the source you posed is really from a .pm file, correct? When you save it as a .pl and feed it directly into perl, it dies with "Can't return outside a subroutine" but I suspect you're really "use"ing it from another file.

    What does the code in that other file look like?

      When I save your code as 'VM_List.pm' and call it like this, it works fine:

      use lib 'c:\temp'; use VM_List; my @testarray = (1,2,3); my $test = VM_List->create(\@testarray); print "test=$test";

      Although calling it with "-Wc" gives me:

      v-string in use/require non-portable at c:\temp/VM_List.pm line 1.

      So I believe that should be a 'require' instead of 'use'. And shouldn't your other use lines come after your package line?

        And shouldn't your other use lines come after your package line?

        Should they?

        For modules that export by default, I want it outside my package space. For strict/warnings, I want them turned on at the earliest oppertunity.

Re: Attempt to free unreferenced scalar
by monachos (Initiate) on Mar 29, 2007 at 09:18 UTC
    Hi, this is new territory for me. I get the error “Attempt to free unreferenced scalar: SV 0x1ebc270, Perl interpreter: 0x223ff4 at lib/DBIx/Lite.pm line 277.” The line in Lite.pm reads “$fh->print( join($args->{'column-separator'}, @FieldNames), $args->{'record-separator'});”
      This is an XS error and should appear if you apply sv_2mortal to a scalar SV more then one time, like sv_2mortal(sv_2mortal(newSViv(0))).