For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > December 2004 > Re: Leak in Win32::ChangeNotify? [solved]









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Leak in Win32::ChangeNotify? [solved]
Thomas Kratz

2004-12-29, 8:56 am

Thomas Kratz wrote:

> === 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 ===


The problem was calling reset on the object even if there was no signalled
change event. Moving the "$notify->reset();" to the end of the "if ( $rc
== 1..." block, ended the leaking.

After looking at the XS code, it was clear that I used the module the
wrong way. Each call to reset() causes a Win32 API call to
FindNextChangeNotification and that only makes sense if the last Find was
successful.

So: no bug, wrong usage!

Thomas

--
$/=$,,$_=<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^.-
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com