in reply to Re^4: panic: attempt to copy freed scalar a7b9650 to ad20598
in thread panic: attempt to copy freed scalar a7b9650 to ad20598

Great! That's an unrelated error that you would need to fix no matter which version of Perl you were using.

You're trying to capturing something, but Perl didn't capture it because it didn't think it needed too. It tries to capture the minimum amount of varaibles.

Note that

try { ... } catch { ... };
is the same as
try(sub { ... }, catch(sub { ... }));

The error occurs with eval EXPR:

$ perl -wle' sub f { my $x = "abc"; sub { eval q{"[$x]"} } }; print f()->(); ' Variable "$x" is not available at (eval 1) line 1. Use of uninitialized value in print at -e line 1. []

The workaround is to make sure the sub references the lexical variables you will need in the eval EXPR:

$ perl -wle' sub f { my $x = "abc"; sub { $x if 0; eval q{"[$x]"} } }; print f()->(); ' [abc]

Replies are listed 'Best First'.
Re^6: panic: attempt to copy freed scalar a7b9650 to ad20598
by mje (Curate) on Dec 03, 2009 at 20:44 UTC

    Thanks ikegami but I'm still not quite understanding this but I certainly appreciate your help. In your example you've used $x for the "workaround". I don't understand why I have to do this. Perhaps it would be easier if I posted the exact code (comments labelled MJE I added):

    sub queue_job { my ($betdb, $q, $job) = @_; my $key; $job->{jobid} = new_job_id($betdb); # MJE - obviously this can be undef but in the case in # question it is usually set my $clientref = $job->{clientref} if (exists($job->{clientref})); # # Insert info about this job in the audit file. # NOTE: This will fail if we've seen this client_reference before +as # client_reference is unique and it would indicate a client has su +bmitted # the same job twice e.g. posted the same job twice. # my $caught = try { my $repost = $clientref ? 0 : undef; $betdb->execProc( $p_run_or_queue_job, {DieOnError => 1, Comment => 'queue_job'}, $job->{jobid}, $clientref, $job->{sessionid}, $job->{name}, {clob => $json->objToData($job)}, $repost, 1); 0; } catch { my $ev = $_; if (($betdb->getDBh())->err == 1) { # constraint violation eval { $betdb->callPkgProc( {DieOnError => 1, Comment => 'run_now'}, BET::DB::SYN_PKG_BETDB, 'p_invalidJobRequest', $job->{jobid}, $clientref, $job->{sessionid}, $job->{name}, $json->objToData($job->{args}), R_DIE, "Job already seen"); 1; } or $log->warn("Failed to mark job already seen - $@"); $job->{joberr} = 'Already seen this job'; $job->{jobnative} = c_JOB_ALREADY_SEEN; $log->warn(sub {'JOB ALREADY POSTED ' . Dumper($job)}); } else { $log->logwarn("Failed to insert into job_audit, $ev"); } 1; }; return if $caught; my @now = gmtime(time()); $job->{jobqed} = sprintf "%d/%d/%d %02d:%02d:%02d", $now[3], $now[4]+1, $now[5]+1900, $now[2], $now[1], $now[0]; push @{$q->{jobs}}, $job; $q->{queued}->{$key} = $job->{jobid} if ($key); $log->debug("Queued job " . $job->{jobid}); $total_jobs_on_all_queues++; return $job->{jobid}; }

    $clientref if the scalar which 5.10.1 complains about.

      my $clientref = $job->{clientref} if (exists($job->{clientref}));
      my with a statement modifier on the end is generally (almost universally?) a bad thing (unless it's a workaround for a weird issue as in ikegami's post). You probably want something like:
      my $clientref = exists($job->{clientref}) ? $job->{clientref} : undef;
      Update: Actually, I don't see any reason to not just do (since the value will be undef if the key doesn't exist, and it won't be autovivified in the hash):
      my $clientref = $job->{clientref};
      Update: ikegami is correct below. His post did not use "my...if...". My bad :-(

        unless it's a workaround for a weird issue as in ikegami's post

        Not only did my post not make use of my ... if ...;, it's always illegal to do so.

        To update ikegami, runrig and BrowserUk. Leaving the code as it was but upgrading from 5.10.0 to 5.10.1 made the panic go away but it was replaced with a Variable "$clientref" is not available. Removing the "if" test when creating $clientref made this warning go away in 5.10.1 and also stopped the panic when I moved back to 5.10.0.

        Many thanks for your help and suggestions.

      I don't understand why I have to do this.

      It's a workaround for a different problem. (You don't use eval EXPR.) I had to guess, since you didn't give me anything but the message to work on.