Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by mje (Curate)
on Dec 03, 2009 at 18:10 UTC ( [id://810901]=note: print w/replies, xml ) Need Help??


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

I have built a default build of 5.10.1. It probably does not match how ubuntu build it (I don't know how they build it). When I run the problem code now I get the following on the console:

Variable "$clientref" is not available at /home/martin/server/q.pl.pan +ics line 1894, <GEN506> line 1.

It is strange as that line is the "};" at the end of the catch block. Although $clientref is referenced in the try block it is created above with a my at the start of the sub containing the try.

Replies are listed 'Best First'.
Re^5: panic: attempt to copy freed scalar a7b9650 to ad20598
by ikegami (Patriarch) on Dec 03, 2009 at 20:07 UTC

    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]

      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 :-(

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-16 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found