Pregunta

I am trying to write a daemon with perl. Now this daemon has the following code

sub b {
    my $data;
    if (some condition) {
         $data->{"endsmeet"} = 1;
    } else {
        $data->{"endsmeet"} = 2;
    }

    my $newData = a($data);
}

sub a {
    my ($data) = @_;
    my %a = ();
    my $newData = {
      endsmeet => undef,
    };
    $a{"boo"} = $data->{"endsmeet"};
    $newData->{"endsmeet"} = \%a;
    return $newData;
}

My question is from the above, does the reference for %a go away and does it get cleaned up when b goes out of scope?

¿Fue útil?

Solución

b returns the value of $newdata, which is a reference to an anon hash, which holds a reference to %a, which holds a scalar in the element with key boo.

If the value returned by b not stored, nothing will be referencing the value of $newdata, so it will get freed, so nothing will be referencing the anon hash, so it will get freed, so nothing will reference the scalar in the element with key boo, so it will get freed.

No cycles. No leak.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top