문제

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