AContext.data can be nil?
- by waza123
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.