Dr. Mu has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the following code in Perl/Tk to keep a button hovering over another app at a certain spot in its window. ($ide is a Win32::CtrlGUI object that refers to the other app.)
sub CheckIDE { my $vis = $ide->get_property('foreground') == $ide ? 1 : 0; my $coord = $ide->get_property('rect'); exit unless defined $coord; if ($vis > $curvis) { $mw->deiconify; $mw->raise } elsif ($vis < $curvis) { $mw->withdraw } $curvis = $vis; my ($rgt, $top) = map {$coord->{$_}} qw/right top/; $rgt += $dx; $top += $dy; $mw->geometry("+$rgt+$top"); $check = $mw->after(10, \&CheckIDE) }
The code works as I want it to, but I can watch the program's memory allocation climb as it runs. I've isolated the apparent memory leak to the
my $coord = $ide->get_property('rect')
statement. It would appear that the hash referenced by $coord is never returned to the heap when this subroutine returns.

Does this analysis sound right? What can I do prevent the ever-increasing memory footprint?

Thanks,
-Phil

Replies are listed 'Best First'.
Re: Memory leak in Win32::CtrlGUI?
by Anonymous Monk on Apr 12, 2011 at 06:40 UTC
      It leaks only when the line
      my $coord = $ide->get_property('rect');
      has not been commented out. The other reference to $ide is not a problem. I suspect the problem is with Win32::Setupsup, but that's just an XS shell, leading me to believe the real culprit is in the lower-level C code. (I'm not a C programmer, BTW.)

      -Phil

        has not been commented out

        That is why I said its plausible. I have vague memories of closures that would leak/stop leaking , randomly, if you added/removed lines

        I suspect the problem is with Win32::Setupsup, but that's just an XS shell, leading me to believe the real culprit is in the lower-level C code. (I'm not a C programmer, BTW.)

        I tend to agree, given this code hasn't been touched in 10+ years.

        I don't see anything obvious in

      Update 2: I gave up on Win32::CtrlGUI. I've rewritten the program to use Win32::GuiTest. Although it wasn't as straightforward to use, it works well, and the memory leak is gone.

      -Phil

Re: Memory leak in Win32::CtrlGUI? [SOLVED, sort of]
by Dr. Mu (Hermit) on Apr 15, 2011 at 05:07 UTC
    Update 2: I gave up on Win32::CtrlGUI. I've rewritten the program to use Win32::GuiTest. Although it wasn't as straightforward to use, it works well, and the memory leak is gone.

    -Phil

Re: Memory leak in Win32::CtrlGUI?
by InfiniteSilence (Curate) on Apr 12, 2011 at 14:30 UTC

    ...I've isolated the apparent memory ...

    This is more of a question than a response but what tools are you using to isolate memory leaks in Perl on Windows? The MSDN articles I've just Googled all seem to be able to locate leaks in .NET programs.

    Celebrate Intellectual Diversity

      I just use Ctrl-Alt-Del to bring up the process monitor, which shows the CPU time percentage and memory footprint for each running process. In the case of a memory leak, the latter figure constantly increases. I was able to isolate the cause by a process of elimination, i.e. commenting out portions of the code until the leak stopped.

      -Phil

Re: Memory leak in Win32::CtrlGUI?
by Dr. Mu (Hermit) on Apr 12, 2011 at 18:29 UTC
    Update:

    If I add the line

    undef %{$coord};
    once I'm finished with $coord, the leak slows down but does not stop. I probably need to dig into the reference a little deeper and undef pieces of it individually.

    -Phil

    Addendum: It turns out the structure doesn't go any deeper. Here's what Data::Dump returned:

    { bottom => 901, left => 432, right => 1182, top => 302 }