Board index » delphi » TServerWinSocket, thread mis-cache and memory leak combo?

TServerWinSocket, thread mis-cache and memory leak combo?

From VCL help (D5.01), TServerClientThread.KeepInCache:

<<
When KeepInCache is True, FreeOnTerminate is set to False before the
thread terminates, and the thread is left in the thread cache.

Quote

If I'm not missing the obvious, this is not the case; once a thread
finishes execution, it is *not* kept in cache, no matter what
KeepInCache is true. From ScktComp.pas,

procedure TServerClientThread.DoTerminate;
  inherited DoTerminate;
  if Assigned(FServerSocket) then
    FServerSocket.RemoveThread(Self);  <-- taking off...
end;

procedure TServerWinSocket.RemoveThread(AThread: TServerClientThread);
begin
  FListLock.Enter;
  try
    FActiveThreads.Remove(AThread);  <-- gone.
  finally
    FListLock.Leave;
  end;
end;

Since the thread object reference is gone for good and
Thread.FreeOnTerminate is false, it also amounts to nice mem leak.

Is this a known problem? Or did I work too late again?

gErgul.

 

Re:TServerWinSocket, thread mis-cache and memory leak combo?


You worked too late again.

You should be looking at TServerClientThread.Execute. You will see that the
while loop only exits if EndConnect is true. EndConenct is only true if the
thread is terminated. (ie. you call Terminate from your execute method) or
KeepInCache is False or an exception is raised. The TServerSocket calls free
for all threads when it is shut down or KeepInCache is false. If you have a
destructor in your thread, it will be called. FreeOnTerminate should not be
changed, as you can see in Scktcomp.pas it is forced to false before it is
freed anyway.

There are no memory leaks in TServerSocket that I have found as yet. I am
processing 8000000 requests per day (LiveStat.com) with no problems.
(ThreadCacheSize of 200 which is never reached, activethreads around 30)

Paul

Quote
"Gokhan Ergul" <ger...@posltd.com.tr> wrote in message

news:3AACD0B5.4610683B@posltd.com.tr...
Quote
> From VCL help (D5.01), TServerClientThread.KeepInCache:

> <<
> When KeepInCache is True, FreeOnTerminate is set to False before the
> thread terminates, and the thread is left in the thread cache.

> If I'm not missing the obvious, this is not the case; once a thread
> finishes execution, it is *not* kept in cache, no matter what
> KeepInCache is true. From ScktComp.pas,

> procedure TServerClientThread.DoTerminate;
>   inherited DoTerminate;
>   if Assigned(FServerSocket) then
>     FServerSocket.RemoveThread(Self);  <-- taking off...
> end;

> procedure TServerWinSocket.RemoveThread(AThread: TServerClientThread);
> begin
>   FListLock.Enter;
>   try
>     FActiveThreads.Remove(AThread);  <-- gone.
>   finally
>     FListLock.Leave;
>   end;
> end;

> Since the thread object reference is gone for good and
> Thread.FreeOnTerminate is false, it also amounts to nice mem leak.

> Is this a known problem? Or did I work too late again?

> gErgul.

Re:TServerWinSocket, thread mis-cache and memory leak combo?


<<Paul:
You should be looking at TServerClientThread.Execute. You will see that
the
while loop only exits if EndConnect is true. EndConenct is only true if
the
thread is terminated. (ie. you call Terminate from your execute method)
or
KeepInCache is False or an exception is raised.

Quote

Erm... Isn't this supposed be that way?
  TWhateverClientThread = class(TServerClientThread)
  ...

  (FreeOnTerminate = False)

procedure TWhateverClientThread.ClientExecute;
begin
  ...
  while (not Terminated) and ClientSocket.Connected do begin
    ... receive request
    ... process request and send response
    Terminate;  <-- request processed, so terminate thread.
    ...
  end;
end;

and since the thread is terminated, EndConnect = True,
TServerClientThread.DoTerminate executes and removes thread from
ServerSocket.FActiveThreads.

The only thing that transpires is, well, don't terminate the thread
after request is served and keep it running. Is that what you mean?

Thanks,

gErgul.

Re:TServerWinSocket, thread mis-cache and memory leak combo?


Yes, you should have the "while not terminated" loop BUT you should NOT call
terminate. You should close the socket which will force the while loop to
exit (socket.connected = false), unless you are implementing a persistent
connection server (ie. http/1.1) where you will stay in your loop until the
client tells you otherwise.

Your while loop (execute method) is actually encapsulated in another while
loop which is controlled by TServerClientThread.Execute. As I said, you must
NOT terminate the thread yourself. Basically you must change your
"Terminate;" instruction to "ClientSocket.Close" and your problems will be
solved.

Paul

Quote
"Gokhan Ergul" <ger...@posltd.com.tr> wrote in message

news:3AACF1B4.7216EB83@posltd.com.tr...
Quote
> <<Paul:
> You should be looking at TServerClientThread.Execute. You will see that
> the
> while loop only exits if EndConnect is true. EndConenct is only true if
> the
> thread is terminated. (ie. you call Terminate from your execute method)
> or
> KeepInCache is False or an exception is raised.

> Erm... Isn't this supposed be that way?

>   TWhateverClientThread = class(TServerClientThread)
>   ...

>   (FreeOnTerminate = False)

> procedure TWhateverClientThread.ClientExecute;
> begin
>   ...
>   while (not Terminated) and ClientSocket.Connected do begin
>     ... receive request
>     ... process request and send response
>     Terminate;  <-- request processed, so terminate thread.
>     ...
>   end;
> end;

> and since the thread is terminated, EndConnect = True,
> TServerClientThread.DoTerminate executes and removes thread from
> ServerSocket.FActiveThreads.

> The only thing that transpires is, well, don't terminate the thread
> after request is served and keep it running. Is that what you mean?

> Thanks,

> gErgul.

Re:TServerWinSocket, thread mis-cache and memory leak combo?


<<Paul:
Yes, you should have the "while not terminated" loop BUT you should NOT call
terminate.

Quote

But that would persistently keep the processor utilized, no?

gErgul.

Re:TServerWinSocket, thread mis-cache and memory leak combo?


No, the thread will go into a suspended state until it is signaled for reuse
(StartConnect). I have emailed you a client/server example which you can
hack apart.

Paul

Quote
"Gokhan Ergul" <ger...@posltd.com.tr> wrote in message

news:3AADDE4A.CC942178@posltd.com.tr...
Quote
> <<Paul:
> Yes, you should have the "while not terminated" loop BUT you should NOT
call
> terminate.

> But that would persistently keep the processor utilized, no?

> gErgul.

Re:TServerWinSocket, thread mis-cache and memory leak combo?


<<Paul:
I have emailed you a client/server example which you can
hack apart.

Quote

Yep, got it. Thank you.

gErgul.

Other Threads