Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Feb : destroy Threadclass and ondisconnect event

www.cryer.info
Managed Newsgroup Archive

destroy Threadclass and ondisconnect event

Subject:destroy Threadclass and ondisconnect event
Posted by:"emre varol" (no..@none.com)
Date:12 Feb 2008 01:49:39

server:tidTcpserver

Server.ThreadClass := TMyPeerThread;


function TMyPeerThread.AddToQueue(Stream: TMemoryStream): Boolean;
begin
Result := False;
try
   if Stream <> nil then
   begin
      with FQueue.LockList do
      try
       try
          Add( Stream );
          Result := True;
       except on e:exception do frmSuperserver.ThreadExceptionLog( e.message + ' test1', 0 );
       end;
      finally
       FQueue.UnlockList;
      end;
   end;
except on e:exception do frmSuperserver.ThreadExceptionLog( e.message + ' test2', 0 );
end;
end;


destructor TMyPeerThread.Destroy;
var i: integer;
     list: Tlist;
     mystream: Tmemorystream;
begin
      if FQueue <> nil then
      begin
          List := FQueue.LockList;
          try
             if List.Count <> 0 then
              for I := 0 to List.Count - 1 do
                try
                  mystream := List.items[ I ];
                  if mystream <> nil then mystream.Free;
                except on e:exception do frmSuperserver.ThreadExceptionLog( e.message + ' test3', 0 );
                end;
             list.Clear;
          finally
              FQueue.UnlockList;
          end;
      end;

      try
        FQueue.Free;
        FWriteLock.Free;
      except on e:exception do frmSuperserver.ThreadExceptionLog( e.message + ' test4', 0 );
      end;
end;


procedure TfrmTcpserver.ServerDisconnect(AThread: TIdPeerThread);
var Ath: TMypeerthread;
     no: byte;
begin
Ath := TMypeerthread( Athread );
no := Ath.no;
if ( no < length( rooms ) ) then
begin
  try
    with rooms[ no ].kritiklist.locklist do
    try
     remove( Athread );
    finally
     rooms[ no ].kritiklist.unlocklist;
    end;
  except  on e:exception do frmSuperserver.ThreadExceptionLog( e.message + ' test5', 0 );
  end;
end;
end;


An other thread want to send any data to all threads these in the rooms[].kritiklist

with rooms[ no ].kritiklist.locklist do
try
if count <> 0 then
  for i := 0 to count - 1 do
  begin
   KAth := TMypeerthread( items[ i ] );
   if items[ i ] <> Athread then
   try
     stream := TMemorystream.create;
     stream.writebuffer( bb, sizeof( bb ) );
     if KAth.AddToQueue( stream ) = false then stream.free;
   except  on e:exception do frmSuperserver.ThreadExceptionLog( e.message + ' test7', 0 );
   end;
  end;
finally
rooms[ no ].kritiklist.unlocklist;
end;


Some times only one exception occurs. This is 'test2' exception that in the AddToQueue function.
Access violation at address 7C81A379 in module 'ntdll.dll'. Write of address
Test2 exception is related with FQueue threadlist.
if KAth.AddToQueue( stream ) ----> if KAth has been free how can we go to its function(AddToQueue)
if KAth has been free I think test7 exception may have occured.
In the TMyPeerThread.Destroy method FQueue threadlist is being free.
But this TMyPeerThread class is being removed from rooms[ no ].kritiklist in ServerDisconnect event.
if ServerDisconnect event is not called by indy and TMyPeerThread.Destroy method is directly called by Indy
yes TMyPeerThread class can not being remove from rooms[ no ].kritiklist.
But in which cases Indy dont call  ServerDisconnect event and directly free the threadclass? I dont catch any
indy exception in serverexecute event. And how can indy decide to free threadclass unless call ondisconnect event.
And FQueue variable are being free only in TMyPeerThread.Destroy method. if destroy method occurs how can we call
KAth.AddToQueue method?


I can not solve this problem.My program continue to run but I want to understand this exception.
help me.I hope I could explain my problem.

Replies:

www.cryer.info
Managed Newsgroup Archive