Leak in Win32::ChangeNotify?

From: Thomas Kratz (ThomasKratz_at_REMOVEwebCAPS.de)
Date: 12/28/04


Date: Tue, 28 Dec 2004 15:42:47 +0100

The code below is a simple example of using Win32::ChangeNotify. AFAIK
this is how it should be used according to the docs.
If you run this you will see a steady increase in the used nonpage pool
memory of the perl process in the Win32 performance monitor (Category
'Process', counter 'Pool Nonpaged Bytes'). Changing the file will result
in the notification message printed to screen as well as in more memory
leaking. After the second change it will go berserk and if you don't
terminate the process quickly it will eat up all the systems nonpaged pool
memory and thus cause serious trouble elsewhere in the system.

Moving the object creation into the while loop and deleting the line with
'$notify->reset()' works around the problem.

Can somebody confirm this? If yes I will file a bug report on rt.cpan.org.

Tested with perl 5.8.4 (custom build) and libwin 0.19.1
(Win32::ChangeNotify 1.02) under WinXP and Win2003 Server.

Thomas

=== code start ===
use strict;
use warnings;

$| = 1;

use File::Spec::Functions qw/catfile/;
use Win32::ChangeNotify;

my $dir = 'd:/tmp/perl/chng';
my $notify_fn = catfile($dir, 'bla.txt');

my $last = 0;
my $notify = Win32::ChangeNotify->new($dir, 0, 'LAST_WRITE');
die 'cannot create notify object'
    unless ref($notify) eq 'Win32::ChangeNotify';

while ( 1 ) {

    my $rc = $notify->wait(100);
    $notify->reset();

    last if $rc == -1;

    my @stat = stat($notify_fn);

    if ( $rc == 1 and $stat[9] > $last ) {

       print "$notify_fn has changed\n";

       $last = $stat[9];
    }
}
=== code end ===

-- 
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-


Relevant Pages

  • Re: Hashtable faster than SQL ?!!
    ... Don't expect any database to be even remotely as fast as a "simple" memory ... object creation) is faster than a simple query against the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array flatten memory "leak"?
    ... but assigning to variables isn't what determines whether memory is used, it's a simple matter of object creation: ... /usr/bin/env ruby ... my point is that what is important is object creation and when those objects get freed by the GC. ...
    (comp.lang.ruby)
  • Re: JLabels Are Big
    ... memory is being used up and what's causing the problem. ... You may have lots of previously made objects (labels) laying around taking up memory, and the gc is thrashing trying to find new memory to allocate. ... turn on both object creation and garbage collection monitoring. ... I'd also turn on Thread Monitoring because it sounds a bit more like you have a run-away thread or something. ...
    (comp.lang.java.programmer)
  • Re: Memory Usage
    ... you might found out that console methods allocate more memory ... Even if you force garbage collection GC is not guaranteed to ... deallocated during object creation and destruction. ...
    (microsoft.public.dotnet.general)