Named Pipe again

Giganews Newsgroups
Subject:Named Pipe again
Posted by: Markus (markus.hu…@freenet.de)
Date:9 Mar 2007

Hello,

I thought I'd got rid of my pipe problems lately with the help from this forum and some changes of my own, but now I've figured out that it isn't so. The application has a huge problem, it leaks about 4K kernel memory every 2 seconds! After several hours or days, depending on memory size the machine halts.

I've nailed it down now to my pipeserver thread which currently looks like this:

procedure TPipeServerThread.Execute;
var
  ByteWritten, DW:DWord;
  portstr        :AnsiString;
begin
  if (PipeHandle = INVALID_HANDLE_VALUE) or
    (Overlapped.hEvent = 0) then Exit;

  portstr := '1234';

  while not Terminated do
  begin
    ConnectNamedPipe(PipeHandle, @Overlapped);

    case WaitForSingleObject(Overlapped.hEvent, 100) of
      WAIT_TIMEOUT: begin
                    end;

      WAIT_OBJECT_0:
      begin
        WriteFile(PipeHandle, PChar(portstr)^, Length(portstr),
                  ByteWritten, @Overlapped);
        sleep(1000);
        DisconnectNamedPipe(PipeHandle);

// if this break is present no kernel memory is leaked, but the // thing obvisiously only runs once. I'd like it to run until
// somebody else terminated the thread but without leaking
// memory. => where exactly does this leaking come from?
break;
      end;

      WAIT_ABANDONED:
      begin
        terminate;
      end;
      WAIT_FAILED:
      begin
        dw:=GetLastError;
      end;
    end;
  end;

  DisconnectNamedPipe(PipeHandle);
end;

So my question is, where exactly does this leakeage come from and how to avoid it whithout making the thread a run once thing as it is right now with that break instruction?

Greetings

Markus

Replies