develooper Front page | perl.perl5.porters | Postings from April 2006

Re: [perl #39017] perlbug AutoReply: Segfault in functionalstreams / bug in closure allocation?

Thread Next
From:
Christian
Date:
April 28, 2006 16:54
Subject:
Re: [perl #39017] perlbug AutoReply: Segfault in functionalstreams / bug in closure allocation?
Message ID:
p04320424c077b5d20d8d@[192.168.40.11]
It seems like it might be a problem with parameter passing through 
@_. The following change prevents the segfault in all cases.


# either use this definition instead:

# sub LengthOfStream ( $ ) {
#     my ($strm)=@_;
#     #@_=();# does not help
#     undef $_[0];# <------ helps
#     my $len=0;
#     while(1) {
# 	my $p= Force($strm);
# 	if ($p eq $Nil) {
# 	    return $len;
# 	} else {
# 	    $len++;
# 	    $strm= Cdr($p);
# 	}
#     }
# }


# or this:

sub LengthOfStream_ {
     my ($strm,$len)=@_;
     #@_=(); # does not help
     undef $_[0];# helps.
     my $p= Force($strm);
     if ($p eq $Nil) {
	$len
     } else {
	@_=(Cdr($p), $len+1);
	goto \&LengthOfStream; # tail call
     }
}

sub LengthOfStream ( $ ) {
     push @_,0;
     goto \&LengthOfStream_;
}


But it has, of course, the ugly side effect of erasing the variable 
being passed by the caller:

my $s= SequenceStream 100000;
print LengthOfStream $s,"\n"; # -> 100001
# but now $s is undef

I find it strange, that this case also requires the undef $_[0] (and 
@_=() is not enough!):

print LengthOfStream SequenceStream 100000,"\n";

Any idea?

What I also don't understand is why this leaks memory and eventually 
terminates because of an out of memory condition (it should just run 
forever):

sub InfiniteStream {
     my ($startnumber)=@_;
     #undef $_[0]; Modification of a read-only value attempted -- of course.
     #@_=();
     Delay {
	Cons $startnumber, InfiniteStream($startnumber+1);
     };
}

LengthOfStream InfiniteStream(0);

Any idea here?

Christian.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About