AContext.data can be nil?

Posted by waza123 on Stack Overflow See other posts from Stack Overflow or by waza123
Published on 2012-11-26T03:09:23Z Indexed on 2012/11/29 17:04 UTC
Read the original article Hit count: 396

Filed under:
|
|

In this code, as you see on Connect, AContext.Data is filled with something

TmyTThreadList = class(TThreadList)
  id: integer;
end;

var unique_id:integer;

procedure TfrmTestIdTCPServer.IdTCPServerConnect(AContext: TIdContext);
 begin
  CS.Enter;
  try
    inc(unique_id);
  finally
    CS.Leave;
  end;

   AContext.Data := myTThreadList.Create;
   list := myTThreadList(AContext.Data).LockList;
   try
     myTThreadList(AContext.Data).id := my_unique_id;
     list.Add(myTThreadList(AContext.Data));
   finally
     myTThreadList(AContext.Data).UnlockList;
   end;
 end;

then on disconnect, coder is checking here for Acontext.Data <> nil

procedure TfrmTestIdTCPServer.IdTCPServerDisconnect(AContext: TIdContext);
 var
 begin
   if AContext.Data <> nil then
   begin

The question is, why he is checking for nil ?

Thanks.

EDIT:

I'm asking this, because when I do the same, onExecute I access AContext.Data , and sometimes (when in same time is connecting many clients) AContext.Data is empty, access violation appears.

© Stack Overflow or respective owner

Related posts about delphi

Related posts about indy