1

In procedure TdwsFileNotifier.Execute, when ReadDirectoryChanges call fails, the thread is terminated by calling Terminate but the loop is not immediately exited. IMO there should be a break immediately after Terminate like this:

   FActive:=True;   
   NameThreadForDebugging('FileNotifier '+AnsiString(FDirectory));
   while not Terminated do begin      
       GetQueuedCompletionStatus(FChangeCP, numBytes, completionKey, FPOverlapped, INFINITE);

       if completionKey<>0 then begin
           if not ReadDirectoryChanges(FDirectoryHandle, @FNotificationBuffer,
                                 SizeOf(TdwsFileNotifierBuffer),
                                 (FMode=dnoDirectoryAndSubTree), FNotifyFilter,
                                 @FBytesWritten, @FOverlapped, nil) then
               Terminate;

     fileOpNotification:=@FNotificationBuffer;
     repeat
        offset:=fileOpNotification^.NextEntryOffset;
        if Assigned(FOnFileChanged) then begin
           notify:=True;
           for i:=0 to High(FIgnoredPaths) do begin
              if StrLComp(@fileOpNotification^.FileName[0],
                          PWideChar(Pointer(FIgnoredPaths[i])),
                          Length(FIgnoredPaths[i]))=0 then begin
                 notify:=False;
                 Break;
              end;
           end;
           if notify then begin
              SetString(fileName, fileOpNotification^.FileName,
                        fileOpNotification^.FileNameLength div SizeOf(Char));
              FQueue.QueueNotification(fileName, fileOpNotification^.Action);
           end;
        end;
        fileOpNotification:=@PAnsiChar(fileOpNotification)[offset];
     until offset=0;
     FBytesWritten:=0;
     FillChar(FNotificationBuffer, 0, SizeOf(TdwsFileNotifierBuffer));

   end else Terminate;
   end;
   FActive:=False;
fpiette
  • 11,983
  • 1
  • 24
  • 46
  • You did not show a loop – David Heffernan Dec 26 '15 at 17:57
  • I tough it was not useful to show complete source code, just where the possible bug... – fpiette Dec 26 '15 at 18:10
  • How could we know whether this is a bug without the context? – David Heffernan Dec 26 '15 at 18:22
  • 1
    Did you ask for support to Eric, directly on DWS side? And yes, the code would definitively be safer using `begin Terminate; break; end;` But IMHO it would not hurt as it is currently written, since the main `while not terminated` loop would break at next iteration, and the thread destructor will wait for the loop to finish. – Arnaud Bouchez Dec 26 '15 at 20:54
  • Eric says on DWScript website to use Stackoverflow for support. So I used it... – fpiette Dec 27 '15 at 12:53

0 Answers0