Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Feb : Re: ThreadClass exception
| Subject: | Re: ThreadClass exception |
| Posted by: | "emre varol" (no..@none.com) |
| Date: | 10 Feb 2008 06:28:46 |
rooms[ rn ].kritik.enter;
with rooms[ rn ].persons.locklist do
try
if count <> 0 then
for i := 0 to count - 1 do
if items[ i ] <> Athread then
begin
stream := Tmemorystream.create;
stream.writebuffer( bb, sizeof( bb ) );
KAth := TMypeerthread( items[ i ] );
try
KAth.AddToQueue( stream );
except on e:exception do .... ----> how is access violation occur?
end;
end;
finally
rooms[ rn ].persons.unlocklist;
rooms[ rn ].kritik.leave;
end;
I couldnt understand indy..
because Athread is being remove from persons list in Tidtcpserver ondisconnect event. And I think in ondisconnect event TMypeerthread is not being free.it is being free After ondisconnect event. So I couldnt understand how this exception occurs. if KAth has been free by indy it must not be in persons list.
"emre varol" <none@none.com> wrote:
>
>I use delphi5 and indy devsnapshot
>
> Tperson = record
> nick: String[ 15 ];
> roomnumber: byte;
> ......
> end;
>
> TMyPeerThread = class(TIdPeerThread)
> private
> FQueue: TThreadList;
> FWriteLock: TCriticalSection;
> public
> person: Tperson;
> constructor Create(ACreateSuspended: Boolean = True);override;
> destructor Destroy; override;
> function AddToQueue(Stream: TStream): Boolean;
> procedure SendQueue;
> end;
>
>constructor TMyPeerThread.Create(ACreateSuspended: Boolean);
>begin
> person.nick := '';
> person.roomnumber := 200;
> .....
> inherited;
>end;
>
>function TMyPeerThread.AddToQueue(Stream: TStream): Boolean;
>begin
> Result := False;
> if Stream <> nil then
> begin
> with FQueue.LockList do
> try
> try
> Add( Stream );
> Result := True;
> except
> end;
> finally
> FQueue.UnlockList;
> end;
> end;
>end;
>
>
>Troom = class
> kritik: TCriticalsection;
> persons: TThreadlist;
> .......
> .....
>end;
>
>
>rooms: array of Troom;
>server: TidTcpserver
>
>....
>setlength( rooms, 10 );
>//create rooms....
>Server.ThreadClass := TMyPeerThread;
>server.Active := true;
>.....
>
>
>procedure TfrmTcpserver.ServerConnect(AThread: TIdPeerThread);
>begin
> try
> Athread.Connection.ReadTimeout := 25000;
> Athread.data := nil;
> TMypeerthread( Athread ).person.IP := AThread.Connection.Socket.Binding.PeerIP;
> except
> end;
>end;
>
>
>procedure TfrmTcpserver.ServerExecute( AThread: TIdPeerThread );
> var firstbyte: byte;
> begin
> try
> TMyPeerThread( AThread ).SendQueue;
>
> if not AThread.Connection.IOHandler.Readable( 160 ) then Exit;
>
> AThread.Connection.ReadBuffer( firstbyte, sizeof( firstbyte ) );
>
> if firstbyte = 100 then Entertheroom( Athread )
> else
> if firstbyte = 101 then sendxx( Athread )
> .....
> except on e:exception do
> if E is Eidexception then raise
> else
> frmserver.ExceptionLoga( e.message + ' boom1 ', 0 );//thread safed
> end;
> end;
>
>
>procedure TfrmTcpserver.ServerDisconnect(AThread: TIdPeerThread);
> var bb: byte;
> begin
> try
> bb := TMypeerthread( Athread ).person.roomnumber;
> if bb < length( rooms ) then
> begin
> rooms[ bb ].kritik.enter;
> with rooms[ bb ].persons.locklist do
> try
> remove( Athread );
> finally
> rooms[ bb ].persons.unlocklist;
> rooms[ bb ].kritik.leave;
> end;
> end;
> ........
> except
> end;
> end;
>
>procedure TfrmTcpserver.Entertheroom( Athread: tidpeerthread );
> var Ath:Tmypeerthread;
> bb: byte;
> begin
> Athread.connection.readbuffer( bb, sizeof( bb ) );
> Ath := TMypeerthread( Athread );
> if bb < length( roomss ) then
> begin
> rooms[ bb ].kritik.enter;
> with rooms[ bb ].persons.locklist do
> try
> add( Athread );
> finally
> rooms[ bb ].persons.unlocklist;
> rooms[ bb ].kritik.leave;
> end;
> end;
> end;
>
>
>procedure TfrmTcpserver.Sendxx( Athread: tidpeerthread );
> var Ath, KAth:Tmypeerthread;
> bb, rn: byte;
> i: integer;
> stream: Tmemorystream;
> begin
> Athread.connection.readbuffer( bb, sizeof( bb ) );
> Ath := TMypeerthread( Athread );
> rn := Ath.person.roomnumber;
> if rn < length( rooms ) then
> begin
> rooms[ rn ].kritik.enter;
> with rooms[ rn ].persons.locklist do
> try
> if count <> 0 then
> for i := 0 to count - 1 do
> if items[ i ] <> Athread then
> begin
> stream := Tmemorystream.create;
> stream.writebuffer( bb, sizeof( bb ) );
> KAth := TMypeerthread( items[ i ] );
> try
> KAth.AddToQueue( stream );
> except on e:exception do .... ------>some times: Access violation at address 7C81A379 in module 'ntdll.dll'. Write of address 312E3843
> end;
> end;
> finally
> rooms[ rn ].persons.unlocklist;
> rooms[ rn ].kritik.leave;
> end;
> end;
> end;
>
>spme times why this exception occur?