質問

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?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top