in reply to unreachable memory with a non-zero reference count

Your code makes no sense:

$retVal = $someObject->bar()->doSomething(); $retVal = \$retVal;

You overwrite $retVal immediately. Maybe you meant:

my $myVal = $someObject->bar()->doSomething(); $retVal = \$myVal; Log4perl->debug('retval: ', sub {$retVal}); return $retVal;

Then, I think you won't have a memory leak, as the only thing that's keeping $myVal alive is the reference in $retVal. When that reference goes out of scope, also $myVal will get released.

Replies are listed 'Best First'.
Re^2: unreachable memory with a non-zero reference count
by Pickwick (Beadle) on Jun 21, 2010 at 13:21 UTC

    Your code makes no sense:
    ...
    You overwrite $retVal immediately. Maybe you meant:

    The objetc's method does return a string, I need a reference to that string, therefore I overwrite the variable with a reference to it's former value just to not need to declare another variable. I found it more readable than the following, which would work, too: $retVal = \$someObject->bar()->doSomething();

      But that way you lose the old value. I'm not sure that that's really what you want:

      #!perl -w use strict; use Data::Dumper; my $foo = 'bar'; $foo = \$foo; print $foo; print $$foo; print Dumper $foo; __END__ REF(0x12da554) REF(0x12da554) $VAR1 = \$VAR1;
        But that way you lose the old value. I'm not sure that that's really what you want:
        Of course it isn't. :-) I thought I would be actually using code like in my example and therefore I thought it would be working, but this doesn't seem to be the case. I can't find it anymore, seems I've rewritten it before running into the problem shown and couldnt' remeber anymore. Seems I now have to reasons for don't using things like that, thanks. :-)