in reply to Re: what is difference between calling the function in Perl/Tk in the following ways
in thread what is difference between calling the function in Perl/Tk in the following ways

I don't see how one could leak and not the other. You could always change
(-command=>sub {&function($arg1,$arg2)});
to
(-command=>[sub {&function($arg1,$arg2)}]);
but I suspect that's already happening internally.
  • Comment on Re^2: what is difference between calling the function in Perl/Tk in the following ways
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: what is difference between calling the function in Perl/Tk in the following ways
by runrig (Abbot) on Apr 16, 2010 at 20:05 UTC
    It's the circular reference preventing garbage collection that would cause the leak. The same sort of leak that happens when you repeatedly create anonymous recursive subs like:
    my $fact; $fact = sub { my $n = shift; return $n if $n <= 1; return $n*fact($n-1); }
    Hmm, you're right, the other one would probably leak also if they both contain $button. (update: oops...looking at wrong thread since the OP didn't even have a circular reference in the example).

      the other one would probably leak also if they both contain $button

      Exactly.

      my $obj; my $obj = { data => [ \&f, \$obj ] }; # leaks my $obj; my $obj = { data => [ sub { f($obj) } ] }; # leaks my $arg; my $obj = { data => [ \&f, \$arg ] }; # doesn't leak my $obj; my $obj = { data => [ sub { f($arg) } ] }; # doesn't leak
Re^3: what is difference between calling the function in Perl/Tk in the following ways
by choroba (Cardinal) on Apr 16, 2010 at 15:02 UTC
    Both these two would leak, I fear. The only safe way is to call
    -command=>[\&function,\$arg1,\$arg2]

      By adding \$arg1,\$arg2, the memory leak goes away? I don't think so.

        Not only by adding the references, but also by including them into a list instead of using them directly. We tested it hundred times, really.