in reply to Re: memory leak when using tail recursion?
in thread memory leak when using tail recursion?
I originally ran into this problem when using Exception::Class to handle errors. I had an exception that caused a tail-recursive call to the current function, and this exception was happening repeatedly because there were a lot of invalid records in a row. Exception::Class objects are pretty large, so the system eventually ran out of memory. The code looked something like this:
Anyway, there are plenty of ways to fix this problem. The easiest is to not use tail-recursion, but it *should* work with tail recursion. The odd thing to me is that this problem only occurs when the function call is in the if-conditional.sub get { my ($self) = @_; my ($record, $e); eval { $record = $self->create_record(); }; if ( $e = Exception::Record::Corrupt->caught() ) { # We got a recoverable error, so skip this record. goto &get; } elsif ( $e = Exception::Class->caught() ) { # Rethrow any generic exception caught and abort. ref $e ? $e->rethrow : die $e; } return $record; }
|
|---|